ScrollSlider.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. internal class ScrollSlider : View
  4. {
  5. public ScrollSlider (Scroll host)
  6. {
  7. _host = host;
  8. Id = "slider";
  9. Width = Dim.Auto (DimAutoStyle.Content);
  10. Height = Dim.Auto (DimAutoStyle.Content);
  11. WantMousePositionReports = true;
  12. }
  13. internal bool _wasSliderMouse;
  14. private readonly Scroll _host;
  15. private int _lastLocation = -1;
  16. private ColorScheme? _savedColorScheme;
  17. /// <inheritdoc/>
  18. public override Attribute GetNormalColor ()
  19. {
  20. if (_savedColorScheme is null)
  21. {
  22. ColorScheme = new () { Normal = new (_host.ColorScheme.HotNormal.Foreground, _host.ColorScheme.HotNormal.Foreground) };
  23. }
  24. else
  25. {
  26. ColorScheme = new () { Normal = new (_host.ColorScheme.Normal.Foreground, _host.ColorScheme.Normal.Foreground) };
  27. }
  28. return base.GetNormalColor ();
  29. }
  30. /// <inheritdoc/>
  31. protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
  32. {
  33. _savedColorScheme ??= _host.ColorScheme;
  34. ColorScheme = new ()
  35. {
  36. Normal = new (_savedColorScheme.HotNormal.Foreground, _savedColorScheme.HotNormal.Foreground),
  37. Focus = new (_savedColorScheme.Focus.Foreground, _savedColorScheme.Focus.Foreground),
  38. HotNormal = new (_savedColorScheme.Normal.Foreground, _savedColorScheme.Normal.Foreground),
  39. HotFocus = new (_savedColorScheme.HotFocus.Foreground, _savedColorScheme.HotFocus.Foreground),
  40. Disabled = new (_savedColorScheme.Disabled.Foreground, _savedColorScheme.Disabled.Foreground)
  41. };
  42. return base.OnMouseEnter (mouseEvent);
  43. }
  44. /// <inheritdoc/>
  45. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  46. {
  47. int location = _host.Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  48. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  49. int barSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
  50. if (mouseEvent.Flags == MouseFlags.Button1Pressed)
  51. {
  52. if (Application.MouseGrabView != this)
  53. {
  54. Application.GrabMouse (this);
  55. _lastLocation = location;
  56. }
  57. }
  58. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  59. {
  60. if (_host.Orientation == Orientation.Vertical)
  61. {
  62. if (Frame.Y + offset >= 0 && Frame.Y + offset + Frame.Height <= barSize)
  63. {
  64. _wasSliderMouse = true;
  65. Y = Frame.Y + offset;
  66. _host.Position = GetPositionFromSliderLocation (Frame.Y);
  67. }
  68. }
  69. else
  70. {
  71. if (Frame.X + offset >= 0 && Frame.X + offset + Frame.Width <= barSize)
  72. {
  73. _wasSliderMouse = true;
  74. X = Frame.X + offset;
  75. _host.Position = GetPositionFromSliderLocation (Frame.X);
  76. }
  77. }
  78. }
  79. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  80. {
  81. _lastLocation = -1;
  82. if (Application.MouseGrabView == this)
  83. {
  84. Application.UngrabMouse ();
  85. }
  86. }
  87. else if ((mouseEvent.Flags == MouseFlags.WheeledDown && _host.Orientation == Orientation.Vertical)
  88. || (mouseEvent.Flags == MouseFlags.WheeledRight && _host.Orientation == Orientation.Horizontal))
  89. {
  90. _host.Position = Math.Min (_host.Position + 1, _host.Size - barSize);
  91. }
  92. else if ((mouseEvent.Flags == MouseFlags.WheeledUp && _host.Orientation == Orientation.Vertical)
  93. || (mouseEvent.Flags == MouseFlags.WheeledLeft && _host.Orientation == Orientation.Horizontal))
  94. {
  95. _host.Position = Math.Max (_host.Position - 1, 0);
  96. }
  97. else if (mouseEvent.Flags != MouseFlags.ReportMousePosition)
  98. {
  99. return base.OnMouseEvent (mouseEvent);
  100. }
  101. return true;
  102. }
  103. /// <inheritdoc/>
  104. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  105. {
  106. if (_savedColorScheme is { } && !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  107. {
  108. ColorScheme = _savedColorScheme;
  109. _savedColorScheme = null;
  110. }
  111. return base.OnMouseLeave (mouseEvent);
  112. }
  113. private int GetPositionFromSliderLocation (int location)
  114. {
  115. if (_host.GetContentSize ().Height == 0 || _host.GetContentSize ().Width == 0)
  116. {
  117. return 0;
  118. }
  119. int scrollSize = _host.Orientation == Orientation.Vertical ? _host.GetContentSize ().Height : _host.GetContentSize ().Width;
  120. // Ensure the Position is valid if the slider is at end
  121. // We use Frame here instead of ContentSize because even if the slider has a margin or border, Frame indicates the actual size
  122. if ((_host.Orientation == Orientation.Vertical && location + Frame.Height == scrollSize)
  123. || (_host.Orientation == Orientation.Horizontal && location + Frame.Width == scrollSize))
  124. {
  125. return _host.Size - scrollSize;
  126. }
  127. return Math.Min ((location * _host.Size + location) / scrollSize, _host.Size - scrollSize);
  128. }
  129. }