ScrollSlider.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. internal class ScrollSlider : View
  4. {
  5. public ScrollSlider ()
  6. {
  7. Id = "scrollSlider";
  8. Width = Dim.Auto (DimAutoStyle.Content);
  9. Height = Dim.Auto (DimAutoStyle.Content);
  10. WantMousePositionReports = true;
  11. }
  12. private int _lastLocation = -1;
  13. private ColorScheme? _savedColorScheme;
  14. public void AdjustSlider ()
  15. {
  16. if (!IsInitialized)
  17. {
  18. return;
  19. }
  20. (int Location, int Dimension) sliderLocationAndDimension = GetSliderLocationDimensionFromPosition ();
  21. X = SuperViewAsScroll.Orientation == Orientation.Vertical ? 0 : sliderLocationAndDimension.Location;
  22. Y = SuperViewAsScroll.Orientation == Orientation.Vertical ? sliderLocationAndDimension.Location : 0;
  23. SetContentSize (
  24. new (
  25. SuperViewAsScroll.Orientation == Orientation.Vertical
  26. ? SuperViewAsScroll.GetContentSize ().Width
  27. : sliderLocationAndDimension.Dimension,
  28. SuperViewAsScroll.Orientation == Orientation.Vertical
  29. ? sliderLocationAndDimension.Dimension
  30. : SuperViewAsScroll.GetContentSize ().Height
  31. ));
  32. SetSliderText ();
  33. }
  34. /// <inheritdoc/>
  35. public override Attribute GetNormalColor ()
  36. {
  37. if (_savedColorScheme is null)
  38. {
  39. ColorScheme = new () { Normal = new (SuperViewAsScroll.ColorScheme.HotNormal.Foreground, SuperViewAsScroll.ColorScheme.HotNormal.Foreground) };
  40. }
  41. else
  42. {
  43. ColorScheme = new () { Normal = new (SuperViewAsScroll.ColorScheme.Normal.Foreground, SuperViewAsScroll.ColorScheme.Normal.Foreground) };
  44. }
  45. return base.GetNormalColor ();
  46. }
  47. /// <inheritdoc/>
  48. protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
  49. {
  50. _savedColorScheme ??= SuperViewAsScroll.ColorScheme;
  51. ColorScheme = new ()
  52. {
  53. Normal = new (_savedColorScheme.HotNormal.Foreground, _savedColorScheme.HotNormal.Foreground),
  54. Focus = new (_savedColorScheme.Focus.Foreground, _savedColorScheme.Focus.Foreground),
  55. HotNormal = new (_savedColorScheme.Normal.Foreground, _savedColorScheme.Normal.Foreground),
  56. HotFocus = new (_savedColorScheme.HotFocus.Foreground, _savedColorScheme.HotFocus.Foreground),
  57. Disabled = new (_savedColorScheme.Disabled.Foreground, _savedColorScheme.Disabled.Foreground)
  58. };
  59. return base.OnMouseEnter (mouseEvent);
  60. }
  61. /// <inheritdoc/>
  62. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  63. {
  64. int location = SuperViewAsScroll.Orientation == Orientation.Vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  65. int offset = _lastLocation > -1 ? location - _lastLocation : 0;
  66. int barSize = SuperViewAsScroll.Orientation == Orientation.Vertical ? SuperViewAsScroll.Viewport.Height : SuperViewAsScroll.Viewport.Width;
  67. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && _lastLocation == -1)
  68. {
  69. if (Application.MouseGrabView != this)
  70. {
  71. Application.GrabMouse (this);
  72. _lastLocation = location;
  73. }
  74. }
  75. else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  76. {
  77. if (SuperViewAsScroll.Orientation == Orientation.Vertical)
  78. {
  79. Y = Frame.Y + offset < 0
  80. ? 0
  81. :
  82. Frame.Y + offset + Frame.Height > barSize + (SuperViewAsScroll.KeepContentInAllViewport ? 0 : barSize)
  83. ?
  84. Math.Max (barSize - Frame.Height, 0)
  85. : Frame.Y + offset;
  86. SuperViewAsScroll.Position = GetPositionFromSliderLocation (Frame.Y);
  87. }
  88. else
  89. {
  90. X = Frame.X + offset < 0 ? 0 :
  91. Frame.X + offset + Frame.Width > barSize ? Math.Max (barSize - Frame.Width, 0) : Frame.X + offset;
  92. SuperViewAsScroll.Position = GetPositionFromSliderLocation (Frame.X);
  93. }
  94. }
  95. else if (mouseEvent.Flags == MouseFlags.Button1Released)
  96. {
  97. _lastLocation = -1;
  98. if (Application.MouseGrabView == this)
  99. {
  100. Application.UngrabMouse ();
  101. }
  102. }
  103. else if ((mouseEvent.Flags == MouseFlags.WheeledDown && SuperViewAsScroll.Orientation == Orientation.Vertical)
  104. || (mouseEvent.Flags == MouseFlags.WheeledRight && SuperViewAsScroll.Orientation == Orientation.Horizontal))
  105. {
  106. SuperViewAsScroll.Position = Math.Min (SuperViewAsScroll.Position + 1, SuperViewAsScroll.Size - barSize);
  107. }
  108. else if ((mouseEvent.Flags == MouseFlags.WheeledUp && SuperViewAsScroll.Orientation == Orientation.Vertical)
  109. || (mouseEvent.Flags == MouseFlags.WheeledLeft && SuperViewAsScroll.Orientation == Orientation.Horizontal))
  110. {
  111. SuperViewAsScroll.Position = Math.Max (SuperViewAsScroll.Position - 1, 0);
  112. }
  113. else if (mouseEvent.Flags != MouseFlags.ReportMousePosition)
  114. {
  115. return base.OnMouseEvent (mouseEvent);
  116. }
  117. return true;
  118. }
  119. /// <inheritdoc/>
  120. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  121. {
  122. if (_savedColorScheme is { } && !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  123. {
  124. ColorScheme = _savedColorScheme;
  125. _savedColorScheme = null;
  126. }
  127. return base.OnMouseLeave (mouseEvent);
  128. }
  129. internal int GetPositionFromSliderLocation (int location)
  130. {
  131. if (SuperViewAsScroll.GetContentSize ().Height == 0 || SuperViewAsScroll.GetContentSize ().Width == 0)
  132. {
  133. return 0;
  134. }
  135. int scrollSize = SuperViewAsScroll.Orientation == Orientation.Vertical
  136. ? SuperViewAsScroll.GetContentSize ().Height
  137. : SuperViewAsScroll.GetContentSize ().Width;
  138. // Ensure the Position is valid if the slider is at end
  139. // We use Frame here instead of ContentSize because even if the slider has a margin or border, Frame indicates the actual size
  140. if ((SuperViewAsScroll.Orientation == Orientation.Vertical && location + Frame.Height >= scrollSize)
  141. || (SuperViewAsScroll.Orientation == Orientation.Horizontal && location + Frame.Width >= scrollSize))
  142. {
  143. return SuperViewAsScroll.Size - scrollSize + (SuperViewAsScroll.KeepContentInAllViewport ? 0 : scrollSize);
  144. }
  145. return (int)Math.Min (Math.Round ((double)(location * SuperViewAsScroll.Size + location) / scrollSize), SuperViewAsScroll.Size - scrollSize);
  146. }
  147. internal (int Location, int Dimension) GetSliderLocationDimensionFromPosition ()
  148. {
  149. if (SuperViewAsScroll.GetContentSize ().Height == 0 || SuperViewAsScroll.GetContentSize ().Width == 0)
  150. {
  151. return new (0, 0);
  152. }
  153. int scrollSize = SuperViewAsScroll.Orientation == Orientation.Vertical
  154. ? SuperViewAsScroll.GetContentSize ().Height
  155. : SuperViewAsScroll.GetContentSize ().Width;
  156. int location;
  157. int dimension;
  158. if (SuperViewAsScroll.Size > 0)
  159. {
  160. dimension = (int)Math.Min (Math.Max (Math.Ceiling ((double)scrollSize * scrollSize / SuperViewAsScroll.Size), 1), scrollSize);
  161. // Ensure the Position is valid
  162. if (SuperViewAsScroll.Position > 0
  163. && SuperViewAsScroll.Position + scrollSize > SuperViewAsScroll.Size + (SuperViewAsScroll.KeepContentInAllViewport ? 0 : scrollSize))
  164. {
  165. SuperViewAsScroll.Position = SuperViewAsScroll.Size - scrollSize;
  166. }
  167. location = (int)Math.Min (Math.Round ((double)SuperViewAsScroll.Position * scrollSize / (SuperViewAsScroll.Size + 1)), scrollSize - dimension);
  168. if (SuperViewAsScroll.Position == SuperViewAsScroll.Size - scrollSize && location + dimension < scrollSize)
  169. {
  170. location = scrollSize - dimension;
  171. }
  172. }
  173. else
  174. {
  175. location = 0;
  176. dimension = scrollSize;
  177. }
  178. return new (location, dimension);
  179. }
  180. // TODO: I think you should create a new `internal` view named "ScrollSlider" with an `Orientation` property. It should inherit from View and override GetNormalColor and the mouse events
  181. // that can be moved within it's Superview, constrained to move only horizontally or vertically depending on Orientation.
  182. // This will really simplify a lot of this.
  183. private void SetSliderText ()
  184. {
  185. TextDirection = SuperViewAsScroll.Orientation == Orientation.Vertical ? TextDirection.TopBottom_LeftRight : TextDirection.LeftRight_TopBottom;
  186. // QUESTION: Should these Glyphs be configurable via CM?
  187. Text = string.Concat (
  188. Enumerable.Repeat (
  189. Glyphs.ContinuousMeterSegment.ToString (),
  190. SuperViewAsScroll.GetContentSize ().Width * SuperViewAsScroll.GetContentSize ().Height));
  191. }
  192. private Scroll SuperViewAsScroll => (SuperView as Scroll)!;
  193. }