Scroll.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Indicates the size of scrollable content and provides a visible element, referred to as the "ScrollSlider" that
  7. /// that is sized to
  8. /// show the proportion of the scrollable content to the size of the <see cref="View.Viewport"/>. The ScrollSlider
  9. /// can be dragged with the mouse. A Scroll can be oriented either vertically or horizontally and is used within a
  10. /// <see cref="ScrollBar"/>.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// By default, this view cannot be focused and does not support keyboard.
  15. /// </para>
  16. /// </remarks>
  17. public class Scroll : View, IOrientation, IDesignable
  18. {
  19. internal readonly ScrollSlider _slider;
  20. /// <inheritdoc/>
  21. public Scroll ()
  22. {
  23. _slider = new ();
  24. Add (_slider);
  25. _slider.FrameChanged += OnSliderOnFrameChanged;
  26. CanFocus = false;
  27. _orientationHelper = new (this); // Do not use object initializer!
  28. _orientationHelper.Orientation = Orientation.Vertical;
  29. _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
  30. _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
  31. // This sets the width/height etc...
  32. OnOrientationChanged (Orientation);
  33. }
  34. /// <inheritdoc />
  35. protected override void OnSubviewLayout (LayoutEventArgs args)
  36. {
  37. if (ViewportDimension < 1)
  38. {
  39. _slider.Size = 1;
  40. return;
  41. }
  42. _slider.Size = (int)Math.Clamp (Math.Floor ((double)ViewportDimension * ViewportDimension / (Size)), 1, ViewportDimension);
  43. }
  44. #region IOrientation members
  45. private readonly OrientationHelper _orientationHelper;
  46. /// <inheritdoc/>
  47. public Orientation Orientation
  48. {
  49. get => _orientationHelper.Orientation;
  50. set => _orientationHelper.Orientation = value;
  51. }
  52. /// <inheritdoc/>
  53. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  54. /// <inheritdoc/>
  55. public event EventHandler<EventArgs<Orientation>>? OrientationChanged;
  56. /// <inheritdoc/>
  57. public void OnOrientationChanged (Orientation newOrientation)
  58. {
  59. TextDirection = Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  60. TextAlignment = Alignment.Center;
  61. VerticalTextAlignment = Alignment.Center;
  62. X = 0;
  63. Y = 0;
  64. if (Orientation == Orientation.Vertical)
  65. {
  66. Width = 1;
  67. Height = Dim.Fill ();
  68. }
  69. else
  70. {
  71. Width = Dim.Fill ();
  72. Height = 1;
  73. }
  74. _slider.Orientation = newOrientation;
  75. }
  76. #endregion
  77. /// <summary>
  78. /// Gets or sets whether the Scroll will show the percentage the slider
  79. /// takes up within the <see cref="Size"/>.
  80. /// </summary>
  81. public bool ShowPercent
  82. {
  83. get => _slider.ShowPercent;
  84. set => _slider.ShowPercent = value;
  85. }
  86. private int ViewportDimension => Orientation == Orientation.Vertical ? Viewport.Height : Viewport.Width;
  87. private int _size;
  88. /// <summary>
  89. /// Gets or sets the total size of the content that can be scrolled.
  90. /// </summary>
  91. public int Size
  92. {
  93. get => _size;
  94. set
  95. {
  96. if (value == _size || value < 0)
  97. {
  98. return;
  99. }
  100. _size = value;
  101. OnSizeChanged (_size);
  102. SizeChanged?.Invoke (this, new (in _size));
  103. }
  104. }
  105. /// <summary>Called when <see cref="Size"/> has changed. </summary>
  106. protected virtual void OnSizeChanged (int size) { }
  107. /// <summary>Raised when <see cref="Size"/> has changed.</summary>
  108. public event EventHandler<EventArgs<int>>? SizeChanged;
  109. private int _position;
  110. private void OnSliderOnFrameChanged (object? sender, EventArgs<Rectangle> args)
  111. {
  112. if (ViewportDimension == 0)
  113. {
  114. return;
  115. }
  116. int framePos = Orientation == Orientation.Vertical ? args.CurrentValue.Y : args.CurrentValue.X;
  117. double pos = ((double)ViewportDimension * ViewportDimension / (Size)) * framePos;
  118. RaisePositionChangeEvents (_position, (int)pos);
  119. }
  120. /// <summary>
  121. /// Gets or sets the position of the start of the Scroll slider, relative to <see cref="Size"/>.
  122. /// </summary>
  123. public int Position
  124. {
  125. get => _position;
  126. set => RaisePositionChangeEvents (_position, value);
  127. }
  128. private void RaisePositionChangeEvents (int current, int value)
  129. {
  130. if (OnPositionChanging (current, value))
  131. {
  132. _slider.Position = current;
  133. return;
  134. }
  135. CancelEventArgs<int> args = new (ref current, ref value);
  136. PositionChanging?.Invoke (this, args);
  137. if (args.Cancel)
  138. {
  139. _slider.Position = current;
  140. return;
  141. }
  142. // This sets the slider position and clamps the value
  143. _slider.Position = value;
  144. if (_slider.Position == _position)
  145. {
  146. return;
  147. }
  148. _position = value;
  149. OnPositionChanged (_position);
  150. PositionChanged?.Invoke (this, new (in value));
  151. }
  152. /// <summary>
  153. /// Called when <see cref="Position"/> is changing. Return true to cancel the change.
  154. /// </summary>
  155. protected virtual bool OnPositionChanging (int currentPos, int newPos)
  156. {
  157. return false;
  158. }
  159. /// <summary>
  160. /// Raised when the <see cref="Position"/> is changing. Set <see cref="CancelEventArgs.Cancel"/> to
  161. /// <see langword="true"/> to prevent the position from being changed.
  162. /// </summary>
  163. public event EventHandler<CancelEventArgs<int>>? PositionChanging;
  164. /// <summary>Called when <see cref="Position"/> has changed.</summary>
  165. protected virtual void OnPositionChanged (int position) { }
  166. /// <summary>Raised when the <see cref="Position"/> has changed.</summary>
  167. public event EventHandler<EventArgs<int>>? PositionChanged;
  168. /// <inheritdoc/>
  169. protected override bool OnClearingViewport ()
  170. {
  171. FillRect (Viewport, Glyphs.Stipple);
  172. return true;
  173. }
  174. /// <inheritdoc />
  175. public bool EnableForDesign ()
  176. {
  177. OrientationChanged += (sender, args) =>
  178. {
  179. if (args.CurrentValue == Orientation.Vertical)
  180. {
  181. Width = 1;
  182. Height = Dim.Fill ();
  183. }
  184. else
  185. {
  186. Width = Dim.Fill ();
  187. Height = 1;
  188. }
  189. };
  190. Width = 1;
  191. Height = Dim.Fill ();
  192. Size = 1000;
  193. Position = 10;
  194. return true;
  195. }
  196. }