View.ScrollBars.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 View constructor.
  9. /// </summary>
  10. private void SetupScrollBars ()
  11. {
  12. if (this is Adornment)
  13. {
  14. return;
  15. }
  16. _verticalScrollBar = new (() => CreateScrollBar (Orientation.Vertical));
  17. _horizontalScrollBar = new (() => CreateScrollBar (Orientation.Horizontal));
  18. }
  19. private ScrollBar CreateScrollBar (Orientation orientation)
  20. {
  21. var scrollBar = new ScrollBar
  22. {
  23. Orientation = orientation,
  24. AutoHide = true
  25. };
  26. if (orientation == Orientation.Vertical)
  27. {
  28. ConfigureVerticalScrollBar (scrollBar);
  29. }
  30. else
  31. {
  32. ConfigureHorizontalScrollBar (scrollBar);
  33. }
  34. Padding?.Add (scrollBar);
  35. scrollBar.Initialized += OnScrollBarInitialized;
  36. return scrollBar;
  37. }
  38. private void ConfigureVerticalScrollBar (ScrollBar scrollBar)
  39. {
  40. scrollBar.X = Pos.AnchorEnd ();
  41. scrollBar.Height = Dim.Fill (Dim.Func (() => _horizontalScrollBar is { IsValueCreated: true, Value.Visible: true } ? 1 : 0));
  42. scrollBar.ScrollableContentSize = GetContentSize ().Height;
  43. ViewportChanged += (_, _) =>
  44. {
  45. scrollBar.VisibleContentSize = Viewport.Height;
  46. scrollBar.Position = Viewport.Y;
  47. };
  48. ContentSizeChanged += (_, _) =>
  49. {
  50. scrollBar.ScrollableContentSize = GetContentSize ().Height;
  51. };
  52. }
  53. private void ConfigureHorizontalScrollBar (ScrollBar scrollBar)
  54. {
  55. scrollBar.Y = Pos.AnchorEnd ();
  56. scrollBar.Width = Dim.Fill (Dim.Func (() => _verticalScrollBar is { IsValueCreated: true, Value.Visible: true } ? 1 : 0));
  57. scrollBar.ScrollableContentSize = GetContentSize ().Width;
  58. ViewportChanged += (_, _) =>
  59. {
  60. scrollBar.VisibleContentSize = Viewport.Width;
  61. scrollBar.Position = Viewport.X;
  62. };
  63. ContentSizeChanged += (_, _) =>
  64. {
  65. scrollBar.ScrollableContentSize = GetContentSize ().Width;
  66. };
  67. }
  68. private void OnScrollBarInitialized (object? sender, EventArgs e)
  69. {
  70. var scrollBar = (ScrollBar)sender!;
  71. if (scrollBar.Orientation == Orientation.Vertical)
  72. {
  73. ConfigureVerticalScrollBarEvents (scrollBar);
  74. }
  75. else
  76. {
  77. ConfigureHorizontalScrollBarEvents (scrollBar);
  78. }
  79. }
  80. private void ConfigureVerticalScrollBarEvents (ScrollBar scrollBar)
  81. {
  82. Padding!.Thickness = Padding.Thickness with { Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : 0 };
  83. scrollBar.PositionChanged += (_, args) =>
  84. {
  85. Viewport = Viewport with
  86. {
  87. Y = Math.Min (args.CurrentValue, GetContentSize ().Height - Viewport.Height)
  88. };
  89. };
  90. scrollBar.VisibleChanged += (_, _) =>
  91. {
  92. Padding.Thickness = Padding.Thickness with
  93. {
  94. Right = scrollBar.Visible ? Padding.Thickness.Right + 1 : Padding.Thickness.Right - 1
  95. };
  96. };
  97. }
  98. private void ConfigureHorizontalScrollBarEvents (ScrollBar scrollBar)
  99. {
  100. Padding!.Thickness = Padding.Thickness with { Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : 0 };
  101. scrollBar.PositionChanged += (_, args) =>
  102. {
  103. Viewport = Viewport with
  104. {
  105. X = Math.Min (args.CurrentValue, GetContentSize ().Width - Viewport.Width)
  106. };
  107. };
  108. scrollBar.VisibleChanged += (_, _) =>
  109. {
  110. Padding.Thickness = Padding.Thickness with
  111. {
  112. Bottom = scrollBar.Visible ? Padding.Thickness.Bottom + 1 : Padding.Thickness.Bottom - 1
  113. };
  114. };
  115. }
  116. /// <summary>
  117. /// </summary>
  118. public ScrollBar HorizontalScrollBar => _horizontalScrollBar.Value;
  119. /// <summary>
  120. /// </summary>
  121. public ScrollBar VerticalScrollBar => _verticalScrollBar.Value;
  122. /// <summary>
  123. /// Clean up the ScrollBars of the View. Called by View.Dispose.
  124. /// </summary>
  125. private void DisposeScrollBars ()
  126. {
  127. if (this is Adornment)
  128. {
  129. return;
  130. }
  131. if (_horizontalScrollBar.IsValueCreated)
  132. {
  133. Padding?.Remove (_horizontalScrollBar.Value);
  134. _horizontalScrollBar.Value.Dispose ();
  135. }
  136. if (_verticalScrollBar.IsValueCreated)
  137. {
  138. Padding?.Remove (_verticalScrollBar.Value);
  139. _verticalScrollBar.Value.Dispose ();
  140. }
  141. }
  142. private void SetScrollBarsKeepContentInAllViewport (ViewportSettings viewportSettings)
  143. {
  144. if (viewportSettings == ViewportSettings.None)
  145. {
  146. //_horizontalScrollBar.Value.KeepContentInAllViewport = true;
  147. //_verticalScrollBar.Value.KeepContentInAllViewport = true;
  148. }
  149. else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
  150. {
  151. _horizontalScrollBar.Value.AutoHide = false;
  152. }
  153. else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
  154. {
  155. _verticalScrollBar.Value.AutoHide = false;
  156. }
  157. else if (viewportSettings.HasFlag (ViewportSettings.AllowNegativeLocation))
  158. {
  159. _horizontalScrollBar.Value.AutoHide = false;
  160. _verticalScrollBar.Value.AutoHide = false;
  161. }
  162. else if (viewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
  163. {
  164. //_horizontalScrollBar.Value.KeepContentInAllViewport = false;
  165. }
  166. else if (viewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
  167. {
  168. //_verticalScrollBar.Value.KeepContentInAllViewport = false;
  169. }
  170. else if (viewportSettings.HasFlag (ViewportSettings.AllowLocationGreaterThanContentSize))
  171. {
  172. //_horizontalScrollBar.Value.KeepContentInAllViewport = false;
  173. //_verticalScrollBar.Value.KeepContentInAllViewport = false;
  174. }
  175. }
  176. }