ScrollBarView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  15. /// scroll position is set at Position.
  16. /// </para>
  17. /// <para>
  18. /// If the region to display the scrollbar is larger than three characters,
  19. /// arrow indicators are drawn.
  20. /// </para>
  21. /// </remarks>
  22. public class ScrollBarView : View {
  23. bool vertical;
  24. int size, position;
  25. bool showScrollIndicator;
  26. bool keepContentAlwaysInViewport = true;
  27. bool autoHideScrollBars = true;
  28. bool hosted;
  29. ScrollBarView otherScrollBarView;
  30. View contentBottomRightCorner;
  31. bool showBothScrollIndicator => OtherScrollBarView != null && OtherScrollBarView.showScrollIndicator && showScrollIndicator;
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  34. /// </summary>
  35. /// <param name="rect">Frame for the scrollbar.</param>
  36. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  39. /// </summary>
  40. /// <param name="rect">Frame for the scrollbar.</param>
  41. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  42. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  43. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  44. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  45. {
  46. Init (size, position, isVertical);
  47. }
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  50. /// </summary>
  51. public ScrollBarView () : this (0, 0, false) { }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  54. /// </summary>
  55. /// <param name="size">The size that this scrollbar represents.</param>
  56. /// <param name="position">The position within this scrollbar.</param>
  57. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  58. public ScrollBarView (int size, int position, bool isVertical) : base ()
  59. {
  60. Init (size, position, isVertical);
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  64. /// </summary>
  65. /// <param name="host">The view that will host this scrollbar.</param>
  66. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  67. /// <param name="showBothScrollIndicator">If set to <c>true (default)</c> will have the other scrollbar, otherwise will have only one.</param>
  68. public ScrollBarView (View host, bool isVertical, bool showBothScrollIndicator = true) : this (0, 0, isVertical)
  69. {
  70. if (host == null) {
  71. throw new ArgumentNullException ("The host parameter can't be null.");
  72. } else if (host.SuperView == null) {
  73. throw new ArgumentNullException ("The host SuperView parameter can't be null.");
  74. }
  75. hosted = true;
  76. ColorScheme = host.ColorScheme;
  77. X = isVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  78. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  79. Host = host;
  80. Host.SuperView.Add (this);
  81. AutoHideScrollBars = true;
  82. if (showBothScrollIndicator) {
  83. OtherScrollBarView = new ScrollBarView (0, 0, !isVertical) {
  84. ColorScheme = host.ColorScheme,
  85. Host = host,
  86. OtherScrollBarView = this,
  87. };
  88. OtherScrollBarView.hosted = true;
  89. OtherScrollBarView.X = OtherScrollBarView.IsVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  90. OtherScrollBarView.Y = OtherScrollBarView.IsVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  91. OtherScrollBarView.Host.SuperView.Add (OtherScrollBarView);
  92. OtherScrollBarView.showScrollIndicator = true;
  93. }
  94. ShowScrollIndicator = true;
  95. contentBottomRightCorner = new View (" ");
  96. Host.SuperView.Add (contentBottomRightCorner);
  97. contentBottomRightCorner.X = Pos.Right (host) - 1;
  98. contentBottomRightCorner.Y = Pos.Bottom (host) - 1;
  99. contentBottomRightCorner.Width = 1;
  100. contentBottomRightCorner.Height = 1;
  101. contentBottomRightCorner.MouseClick += ContentBottomRightCorner_MouseClick;
  102. }
  103. void ContentBottomRightCorner_MouseClick (MouseEventArgs me)
  104. {
  105. if (me.MouseEvent.Flags == MouseFlags.WheeledDown || me.MouseEvent.Flags == MouseFlags.WheeledUp
  106. || me.MouseEvent.Flags == MouseFlags.WheeledRight || me.MouseEvent.Flags == MouseFlags.WheeledLeft) {
  107. me.Handled = true;
  108. MouseEvent (me.MouseEvent);
  109. } else if (me.MouseEvent.Flags == MouseFlags.Button1Clicked) {
  110. me.Handled = true;
  111. Host.SetFocus ();
  112. }
  113. }
  114. void Init (int size, int position, bool isVertical)
  115. {
  116. vertical = isVertical;
  117. this.position = position;
  118. this.size = size;
  119. WantContinuousButtonPressed = true;
  120. }
  121. /// <summary>
  122. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  123. /// </summary>
  124. public bool IsVertical {
  125. get => vertical;
  126. set {
  127. vertical = value;
  128. SetNeedsDisplay ();
  129. }
  130. }
  131. /// <summary>
  132. /// The size of content the scrollbar represents.
  133. /// </summary>
  134. /// <value>The size.</value>
  135. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  136. /// part of a <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.</remarks>
  137. public int Size {
  138. get => size;
  139. set {
  140. if (hosted || (otherScrollBarView != null && otherScrollBarView.hosted)) {
  141. size = value + 1;
  142. } else {
  143. size = value;
  144. }
  145. SetNeedsDisplay ();
  146. }
  147. }
  148. /// <summary>
  149. /// This event is raised when the position on the scrollbar has changed.
  150. /// </summary>
  151. public event Action ChangedPosition;
  152. /// <summary>
  153. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  154. /// </summary>
  155. /// <value>The position.</value>
  156. public int Position {
  157. get => position;
  158. set {
  159. if (position != value) {
  160. if (CanScroll (value - position, out int max, vertical)) {
  161. if (max == value - position) {
  162. position = value;
  163. } else {
  164. position = Math.Max (position + max, 0);
  165. }
  166. } else if (max < 0) {
  167. position = Math.Max (position + max, 0);
  168. }
  169. var s = GetBarsize (vertical);
  170. if (position + s == size && (hosted || (otherScrollBarView != null && otherScrollBarView.hosted))) {
  171. position++;
  172. }
  173. OnChangedPosition ();
  174. SetNeedsDisplay ();
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// Get or sets the view that host this <see cref="View"/>
  180. /// </summary>
  181. public View Host { get; internal set; }
  182. /// <summary>
  183. /// Represent a vertical or horizontal ScrollBarView other than this.
  184. /// </summary>
  185. public ScrollBarView OtherScrollBarView {
  186. get => otherScrollBarView;
  187. set {
  188. if (value != null && (value.IsVertical && vertical || !value.IsVertical && !vertical)) {
  189. throw new ArgumentException ($"There is already a {(vertical ? "vertical" : "horizontal")} ScrollBarView.");
  190. }
  191. otherScrollBarView = value;
  192. }
  193. }
  194. /// <summary>
  195. /// Gets or sets the visibility for the vertical or horizontal scroll indicator.
  196. /// </summary>
  197. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  198. public bool ShowScrollIndicator {
  199. get => showScrollIndicator;
  200. set {
  201. if (value == showScrollIndicator) {
  202. return;
  203. }
  204. showScrollIndicator = value;
  205. SetNeedsLayout ();
  206. if (value) {
  207. Visible = true;
  208. } else {
  209. Visible = false;
  210. Position = 0;
  211. }
  212. SetWidthHeight ();
  213. }
  214. }
  215. /// <summary>
  216. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/>
  217. /// </summary>
  218. public bool KeepContentAlwaysInViewport {
  219. get { return keepContentAlwaysInViewport; }
  220. set {
  221. if (keepContentAlwaysInViewport != value) {
  222. keepContentAlwaysInViewport = value;
  223. int pos = 0;
  224. if (value && !vertical && position + Host.Bounds.Width > size) {
  225. pos = size - Host.Bounds.Width + (showBothScrollIndicator ? 1 : 0);
  226. }
  227. if (value && vertical && position + Host.Bounds.Height > size) {
  228. pos = size - Host.Bounds.Height + (showBothScrollIndicator ? 1 : 0);
  229. }
  230. if (pos != 0) {
  231. Position = pos;
  232. }
  233. if (OtherScrollBarView != null && OtherScrollBarView.keepContentAlwaysInViewport != value) {
  234. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  235. }
  236. if (pos == 0) {
  237. Refresh ();
  238. }
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  244. /// </summary>
  245. public bool AutoHideScrollBars {
  246. get => autoHideScrollBars;
  247. set {
  248. if (autoHideScrollBars != value) {
  249. autoHideScrollBars = value;
  250. SetNeedsDisplay ();
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
  256. /// </summary>
  257. public virtual void OnChangedPosition ()
  258. {
  259. ChangedPosition?.Invoke ();
  260. }
  261. /// <summary>
  262. /// Only used for a hosted view that will update and redraw the scrollbars.
  263. /// </summary>
  264. public virtual void Refresh ()
  265. {
  266. ShowHideScrollBars ();
  267. }
  268. void ShowHideScrollBars ()
  269. {
  270. if (!hosted || (hosted && !autoHideScrollBars)) {
  271. if (contentBottomRightCorner != null && contentBottomRightCorner.Visible) {
  272. contentBottomRightCorner.Visible = false;
  273. }
  274. return;
  275. }
  276. var pending = CheckBothScrollBars (this);
  277. if (otherScrollBarView != null) {
  278. CheckBothScrollBars (otherScrollBarView, pending);
  279. }
  280. SetWidthHeight ();
  281. SetRelativeLayout (Bounds);
  282. if (otherScrollBarView != null) {
  283. OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds);
  284. }
  285. if (showBothScrollIndicator) {
  286. if (contentBottomRightCorner != null) {
  287. contentBottomRightCorner.Visible = true;
  288. }
  289. } else if (!showScrollIndicator) {
  290. if (contentBottomRightCorner != null) {
  291. contentBottomRightCorner.Visible = false;
  292. }
  293. if (Application.mouseGrabView != null && Application.mouseGrabView == this) {
  294. Application.UngrabMouse ();
  295. }
  296. } else {
  297. contentBottomRightCorner.Visible = false;
  298. }
  299. if (showScrollIndicator) {
  300. Redraw (Bounds);
  301. }
  302. if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) {
  303. otherScrollBarView.Redraw (otherScrollBarView.Bounds);
  304. }
  305. }
  306. bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  307. {
  308. int barsize = scrollBarView.vertical ? scrollBarView.Bounds.Height : scrollBarView.Bounds.Width;
  309. if (barsize == 0 || barsize > scrollBarView.size) {
  310. if (scrollBarView.showScrollIndicator) {
  311. scrollBarView.ShowScrollIndicator = false;
  312. }
  313. } else if (barsize > 0 && barsize == scrollBarView.size && scrollBarView.OtherScrollBarView != null && pending) {
  314. if (scrollBarView.showScrollIndicator) {
  315. scrollBarView.ShowScrollIndicator = false;
  316. }
  317. if (scrollBarView.OtherScrollBarView != null && scrollBarView.showBothScrollIndicator) {
  318. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  319. }
  320. } else if (barsize > 0 && barsize == size && scrollBarView.OtherScrollBarView != null && !pending) {
  321. pending = true;
  322. } else {
  323. if (scrollBarView.OtherScrollBarView != null && pending) {
  324. if (!scrollBarView.showBothScrollIndicator) {
  325. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  326. }
  327. }
  328. if (!scrollBarView.showScrollIndicator) {
  329. scrollBarView.ShowScrollIndicator = true;
  330. }
  331. }
  332. return pending;
  333. }
  334. void SetWidthHeight ()
  335. {
  336. if (showBothScrollIndicator) {
  337. Width = vertical ? 1 : Dim.Width (Host) - 1;
  338. Height = vertical ? Dim.Height (Host) - 1 : 1;
  339. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 1;
  340. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 1 : 1;
  341. } else if (showScrollIndicator) {
  342. Width = vertical ? 1 : Dim.Width (Host) - 0;
  343. Height = vertical ? Dim.Height (Host) - 0 : 1;
  344. } else if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) {
  345. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 0;
  346. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 0 : 1;
  347. }
  348. }
  349. int posTopTee;
  350. int posLeftTee;
  351. int posBottomTee;
  352. int posRightTee;
  353. ///<inheritdoc/>
  354. public override void Redraw (Rect region)
  355. {
  356. if (ColorScheme == null || Size == 0) {
  357. return;
  358. }
  359. Driver.SetAttribute (ColorScheme.Normal);
  360. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  361. return;
  362. }
  363. if (vertical) {
  364. if (region.Right < Bounds.Width - 1) {
  365. return;
  366. }
  367. var col = Bounds.Width - 1;
  368. var bh = Bounds.Height;
  369. Rune special;
  370. if (bh < 4) {
  371. var by1 = position * bh / Size;
  372. var by2 = (position + bh) * bh / Size;
  373. Move (col, 0);
  374. if (Bounds.Height == 1) {
  375. Driver.AddRune (Driver.Diamond);
  376. } else {
  377. Driver.AddRune (Driver.UpArrow);
  378. }
  379. if (Bounds.Height == 3) {
  380. Move (col, 1);
  381. Driver.AddRune (Driver.Diamond);
  382. }
  383. if (Bounds.Height > 1) {
  384. Move (col, Bounds.Height - 1);
  385. Driver.AddRune (Driver.DownArrow);
  386. }
  387. } else {
  388. bh -= 2;
  389. var by1 = KeepContentAlwaysInViewport ? position * bh / Size : position * bh / (Size + bh);
  390. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / (Size + bh);
  391. if (KeepContentAlwaysInViewport && by1 == by2) {
  392. by1 = Math.Max (by1 - 1, 0);
  393. }
  394. Move (col, 0);
  395. Driver.AddRune (Driver.UpArrow);
  396. Move (col, Bounds.Height - 1);
  397. Driver.AddRune (Driver.DownArrow);
  398. bool hasTopTee = false;
  399. bool hasDiamond = false;
  400. bool hasBottomTee = false;
  401. for (int y = 0; y < bh; y++) {
  402. Move (col, y + 1);
  403. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  404. special = Driver.Stipple;
  405. } else {
  406. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  407. hasDiamond = true;
  408. special = Driver.Diamond;
  409. } else {
  410. if (y == by1 && !hasTopTee) {
  411. hasTopTee = true;
  412. posTopTee = y;
  413. special = Driver.TopTee;
  414. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  415. hasBottomTee = true;
  416. posBottomTee = y;
  417. special = Driver.BottomTee;
  418. } else {
  419. special = Driver.VLine;
  420. }
  421. }
  422. }
  423. Driver.AddRune (special);
  424. }
  425. if (!hasTopTee) {
  426. Move (col, Bounds.Height - 2);
  427. Driver.AddRune (Driver.TopTee);
  428. }
  429. }
  430. } else {
  431. if (region.Bottom < Bounds.Height - 1) {
  432. return;
  433. }
  434. var row = Bounds.Height - 1;
  435. var bw = Bounds.Width;
  436. Rune special;
  437. if (bw < 4) {
  438. var bx1 = position * bw / Size;
  439. var bx2 = (position + bw) * bw / Size;
  440. Move (0, row);
  441. Driver.AddRune (Driver.LeftArrow);
  442. Driver.AddRune (Driver.RightArrow);
  443. } else {
  444. bw -= 2;
  445. var bx1 = KeepContentAlwaysInViewport ? position * bw / Size : position * bw / (Size + bw);
  446. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / (Size + bw);
  447. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  448. bx1 = Math.Max (bx1 - 1, 0);
  449. }
  450. Move (0, row);
  451. Driver.AddRune (Driver.LeftArrow);
  452. bool hasLeftTee = false;
  453. bool hasDiamond = false;
  454. bool hasRightTee = false;
  455. for (int x = 0; x < bw; x++) {
  456. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  457. special = Driver.Stipple;
  458. } else {
  459. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  460. hasDiamond = true;
  461. special = Driver.Diamond;
  462. } else {
  463. if (x == bx1 && !hasLeftTee) {
  464. hasLeftTee = true;
  465. posLeftTee = x;
  466. special = Driver.LeftTee;
  467. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  468. hasRightTee = true;
  469. posRightTee = x;
  470. special = Driver.RightTee;
  471. } else {
  472. special = Driver.HLine;
  473. }
  474. }
  475. }
  476. Driver.AddRune (special);
  477. }
  478. if (!hasLeftTee) {
  479. Move (Bounds.Width - 2, row);
  480. Driver.AddRune (Driver.LeftTee);
  481. }
  482. Driver.AddRune (Driver.RightArrow);
  483. }
  484. }
  485. if (contentBottomRightCorner != null && hosted && showBothScrollIndicator) {
  486. contentBottomRightCorner.Redraw (contentBottomRightCorner.Bounds);
  487. }
  488. }
  489. int lastLocation = -1;
  490. int posBarOffset;
  491. ///<inheritdoc/>
  492. public override bool MouseEvent (MouseEvent me)
  493. {
  494. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1DoubleClicked &&
  495. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) &&
  496. me.Flags != MouseFlags.Button1Released && me.Flags != MouseFlags.WheeledDown &&
  497. me.Flags != MouseFlags.WheeledUp && me.Flags != MouseFlags.WheeledRight &&
  498. me.Flags != MouseFlags.WheeledLeft && me.Flags != MouseFlags.Button1TripleClicked) {
  499. return false;
  500. }
  501. if (Host != null && !Host.HasFocus) {
  502. Host.SetFocus ();
  503. }
  504. int location = vertical ? me.Y : me.X;
  505. int barsize = vertical ? Bounds.Height : Bounds.Width;
  506. int posTopLeftTee = vertical ? posTopTee + 1 : posLeftTee + 1;
  507. int posBottomRightTee = vertical ? posBottomTee + 1 : posRightTee + 1;
  508. barsize -= 2;
  509. var pos = Position;
  510. if (me.Flags != MouseFlags.Button1Released
  511. && (Application.mouseGrabView == null || Application.mouseGrabView != this)) {
  512. Application.GrabMouse (this);
  513. } else if (me.Flags == MouseFlags.Button1Released && Application.mouseGrabView != null && Application.mouseGrabView == this) {
  514. lastLocation = -1;
  515. Application.UngrabMouse ();
  516. return true;
  517. }
  518. if (showScrollIndicator && (me.Flags == MouseFlags.WheeledDown || me.Flags == MouseFlags.WheeledUp ||
  519. me.Flags == MouseFlags.WheeledRight || me.Flags == MouseFlags.WheeledLeft)) {
  520. return Host.MouseEvent (me);
  521. }
  522. if (location == 0) {
  523. if (pos > 0) {
  524. Position = pos - 1;
  525. }
  526. } else if (location == barsize + 1) {
  527. if (CanScroll (1, out _, vertical)) {
  528. Position = pos + 1;
  529. }
  530. } else if (location > 0 && location < barsize + 1) {
  531. //var b1 = pos * (Size > 0 ? barsize / Size : 0);
  532. //var b2 = Size > 0
  533. // ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  534. // : 0;
  535. //if (KeepContentAlwaysInViewport && b1 == b2) {
  536. // b1 = Math.Max (b1 - 1, 0);
  537. //}
  538. if (lastLocation > -1 || (location >= posTopLeftTee && location <= posBottomRightTee
  539. && me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  540. if (lastLocation == -1) {
  541. lastLocation = location;
  542. posBarOffset = keepContentAlwaysInViewport ? Math.Max (location - posTopLeftTee, 1) : 0;
  543. return true;
  544. }
  545. if (location > lastLocation) {
  546. if (location - posBarOffset < barsize) {
  547. var np = ((location - posBarOffset) * Size / barsize) + (Size / barsize);
  548. if (CanScroll (np - pos, out int nv, vertical)) {
  549. Position = pos + nv;
  550. }
  551. } else if (CanScroll (Size - pos, out int nv, vertical)) {
  552. Position = Math.Min (pos + nv, Size);
  553. }
  554. } else if (location < lastLocation) {
  555. if (location - posBarOffset > 0) {
  556. var np = ((location - posBarOffset) * Size / barsize) - (Size / barsize);
  557. if (CanScroll (np - pos, out int nv, vertical)) {
  558. Position = pos + nv;
  559. }
  560. } else {
  561. Position = 0;
  562. }
  563. } else if (location - posBarOffset >= barsize && posBottomRightTee - posTopLeftTee >= 3 && CanScroll (Size - pos, out int nv, vertical)) {
  564. Position = Math.Min (pos + nv, Size);
  565. } else if (location - posBarOffset >= barsize - 1 && posBottomRightTee - posTopLeftTee <= 3 && CanScroll (Size - pos, out nv, vertical)) {
  566. Position = Math.Min (pos + nv, Size);
  567. } else if (location - posBarOffset <= 0 && posBottomRightTee - posTopLeftTee <= 3) {
  568. Position = 0;
  569. }
  570. } else if (location > posBottomRightTee) {
  571. if (CanScroll (barsize, out int nv, vertical)) {
  572. Position = pos + nv;
  573. }
  574. } else if (location < posTopLeftTee) {
  575. if (CanScroll (-barsize, out int nv, vertical)) {
  576. Position = pos + nv;
  577. }
  578. } else if (location == 1 && posTopLeftTee <= 3) {
  579. Position = 0;
  580. } else if (location == barsize) {
  581. if (CanScroll (Size - pos, out int nv, vertical)) {
  582. Position = Math.Min (pos + nv, Size);
  583. }
  584. }
  585. }
  586. return true;
  587. }
  588. internal bool CanScroll (int n, out int max, bool isVertical = false)
  589. {
  590. if (Host?.Bounds.IsEmpty != false) {
  591. max = 0;
  592. return false;
  593. }
  594. int s = GetBarsize (isVertical);
  595. var newSize = Math.Max (Math.Min (size - s, position + n), 0);
  596. max = size > s + newSize ? (newSize == 0 ? -position : n) : size - (s + position) - 1;
  597. if (size >= s + newSize && max != 0) {
  598. return true;
  599. }
  600. return false;
  601. }
  602. int GetBarsize (bool isVertical)
  603. {
  604. if (Host?.Bounds.IsEmpty != false) {
  605. return 0;
  606. }
  607. return isVertical ?
  608. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  609. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  610. }
  611. ///<inheritdoc/>
  612. public override bool OnEnter (View view)
  613. {
  614. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  615. return base.OnEnter (view);
  616. }
  617. }
  618. }