ScrollView.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. ///
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// The subviews that are added to this scrollview are offset by the
  9. /// ContentOffset property. The view itself is a window into the
  10. /// space represented by the ContentSize.
  11. /// </para>
  12. /// <para>
  13. ///
  14. /// </para>
  15. /// </remarks>
  16. public class ScrollView : View {
  17. View contentView;
  18. public ScrollView (Rect frame) : base (frame)
  19. {
  20. contentView = new View (frame);
  21. base.Add (contentView);
  22. }
  23. Size contentSize;
  24. Point contentOffset;
  25. bool showHorizontalScrollIndicator;
  26. bool showVerticalScrollIndicator;
  27. /// <summary>
  28. /// Represents the contents of the data shown inside the scrolview
  29. /// </summary>
  30. /// <value>The size of the content.</value>
  31. public Size ContentSize {
  32. get {
  33. return contentSize;
  34. }
  35. set {
  36. contentSize = value;
  37. contentView.Frame = new Rect (contentOffset, value);
  38. }
  39. }
  40. /// <summary>
  41. /// Represents the top left corner coordinate that is displayed by the scrollview
  42. /// </summary>
  43. /// <value>The content offset.</value>
  44. public Point ContentOffset {
  45. get {
  46. return contentOffset;
  47. }
  48. set {
  49. contentOffset = new Point (-value.X, -value.Y);
  50. contentView.Frame = new Rect (contentOffset, contentSize);
  51. }
  52. }
  53. /// <summary>
  54. /// Adds the view to the scrollview.
  55. /// </summary>
  56. /// <param name="view">The view to add to the scrollview.</param>
  57. public override void Add (View view)
  58. {
  59. contentView.Add (view);
  60. }
  61. /// <summary>
  62. /// Gets or sets the visibility for the horizontal scroll indicator.
  63. /// </summary>
  64. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  65. public bool ShowHorizontalScrollIndicator {
  66. get => showHorizontalScrollIndicator;
  67. set {
  68. showHorizontalScrollIndicator = value;
  69. SetNeedsDisplay ();
  70. }
  71. }
  72. /// <summary>
  73. /// /// Gets or sets the visibility for the vertical scroll indicator.
  74. /// </summary>
  75. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  76. public bool ShowVerticalScrollIndicator {
  77. get => showVerticalScrollIndicator;
  78. set {
  79. showVerticalScrollIndicator = value;
  80. SetNeedsDisplay ();
  81. }
  82. }
  83. /// <summary>
  84. /// This event is raised when the contents have scrolled
  85. /// </summary>
  86. public event Action<ScrollView> Scrolled;
  87. public override void Redraw(Rect region)
  88. {
  89. var oldClip = ClipToBounds ();
  90. base.Redraw(region);
  91. Driver.SetAttribute (ColorScheme.Normal);
  92. DrawFrame (Bounds);
  93. Driver.Clip = oldClip;
  94. }
  95. }
  96. }