ScrollBarView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. return;
  272. }
  273. var pending = CheckBothScrollBars (this);
  274. CheckBothScrollBars (otherScrollBarView, pending);
  275. SetWidthHeight ();
  276. SetRelativeLayout (Bounds);
  277. OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds);
  278. if (showBothScrollIndicator) {
  279. if (contentBottomRightCorner != null) {
  280. contentBottomRightCorner.Visible = true;
  281. }
  282. } else if (!showScrollIndicator) {
  283. if (contentBottomRightCorner != null) {
  284. contentBottomRightCorner.Visible = false;
  285. }
  286. if (Application.mouseGrabView != null && Application.mouseGrabView == this) {
  287. Application.UngrabMouse ();
  288. }
  289. }
  290. if (showScrollIndicator) {
  291. Redraw (Bounds);
  292. }
  293. if (otherScrollBarView.showScrollIndicator) {
  294. otherScrollBarView.Redraw (otherScrollBarView.Bounds);
  295. }
  296. }
  297. bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  298. {
  299. int barsize = scrollBarView.vertical ? scrollBarView.Bounds.Height : scrollBarView.Bounds.Width;
  300. if (barsize == 0 || barsize > scrollBarView.size) {
  301. if (scrollBarView.showScrollIndicator) {
  302. scrollBarView.ShowScrollIndicator = false;
  303. }
  304. } else if (barsize > 0 && barsize == scrollBarView.size && scrollBarView.OtherScrollBarView != null && pending) {
  305. if (scrollBarView.showScrollIndicator) {
  306. scrollBarView.ShowScrollIndicator = false;
  307. }
  308. if (scrollBarView.OtherScrollBarView != null && scrollBarView.showBothScrollIndicator) {
  309. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  310. }
  311. } else if (barsize > 0 && barsize == size && scrollBarView.OtherScrollBarView != null && !pending) {
  312. pending = true;
  313. } else {
  314. if (scrollBarView.OtherScrollBarView != null && pending) {
  315. if (!scrollBarView.showBothScrollIndicator) {
  316. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  317. }
  318. }
  319. if (!scrollBarView.showScrollIndicator) {
  320. scrollBarView.ShowScrollIndicator = true;
  321. }
  322. }
  323. return pending;
  324. }
  325. void SetWidthHeight ()
  326. {
  327. if (showBothScrollIndicator) {
  328. Width = vertical ? 1 : Dim.Width (Host) - 1;
  329. Height = vertical ? Dim.Height (Host) - 1 : 1;
  330. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 1;
  331. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 1 : 1;
  332. } else if (showScrollIndicator) {
  333. Width = vertical ? 1 : Dim.Width (Host) - 0;
  334. Height = vertical ? Dim.Height (Host) - 0 : 1;
  335. } else if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) {
  336. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 0;
  337. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 0 : 1;
  338. }
  339. }
  340. int posTopTee;
  341. int posLeftTee;
  342. int posBottomTee;
  343. int posRightTee;
  344. ///<inheritdoc/>
  345. public override void Redraw (Rect region)
  346. {
  347. if (ColorScheme == null || Size == 0) {
  348. return;
  349. }
  350. Driver.SetAttribute (ColorScheme.Normal);
  351. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  352. return;
  353. }
  354. if (vertical) {
  355. if (region.Right < Bounds.Width - 1) {
  356. return;
  357. }
  358. var col = Bounds.Width - 1;
  359. var bh = Bounds.Height;
  360. Rune special;
  361. if (bh < 4) {
  362. var by1 = position * bh / Size;
  363. var by2 = (position + bh) * bh / Size;
  364. Move (col, 0);
  365. if (Bounds.Height == 1) {
  366. Driver.AddRune (Driver.Diamond);
  367. } else {
  368. Driver.AddRune (Driver.UpArrow);
  369. }
  370. if (Bounds.Height == 3) {
  371. Move (col, 1);
  372. Driver.AddRune (Driver.Diamond);
  373. }
  374. if (Bounds.Height > 1) {
  375. Move (col, Bounds.Height - 1);
  376. Driver.AddRune (Driver.DownArrow);
  377. }
  378. } else {
  379. bh -= 2;
  380. var by1 = KeepContentAlwaysInViewport ? position * bh / Size : position * bh / (Size + bh);
  381. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / (Size + bh);
  382. if (KeepContentAlwaysInViewport && by1 == by2) {
  383. by1 = Math.Max (by1 - 1, 0);
  384. }
  385. Move (col, 0);
  386. Driver.AddRune (Driver.UpArrow);
  387. Move (col, Bounds.Height - 1);
  388. Driver.AddRune (Driver.DownArrow);
  389. bool hasTopTee = false;
  390. bool hasDiamond = false;
  391. bool hasBottomTee = false;
  392. for (int y = 0; y < bh; y++) {
  393. Move (col, y + 1);
  394. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  395. special = Driver.Stipple;
  396. } else {
  397. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  398. hasDiamond = true;
  399. special = Driver.Diamond;
  400. } else {
  401. if (y == by1 && !hasTopTee) {
  402. hasTopTee = true;
  403. posTopTee = y;
  404. special = Driver.TopTee;
  405. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  406. hasBottomTee = true;
  407. posBottomTee = y;
  408. special = Driver.BottomTee;
  409. } else {
  410. special = Driver.VLine;
  411. }
  412. }
  413. }
  414. Driver.AddRune (special);
  415. }
  416. if (!hasTopTee) {
  417. Move (col, Bounds.Height - 2);
  418. Driver.AddRune (Driver.TopTee);
  419. }
  420. }
  421. } else {
  422. if (region.Bottom < Bounds.Height - 1) {
  423. return;
  424. }
  425. var row = Bounds.Height - 1;
  426. var bw = Bounds.Width;
  427. Rune special;
  428. if (bw < 4) {
  429. var bx1 = position * bw / Size;
  430. var bx2 = (position + bw) * bw / Size;
  431. Move (0, row);
  432. Driver.AddRune (Driver.LeftArrow);
  433. Driver.AddRune (Driver.RightArrow);
  434. } else {
  435. bw -= 2;
  436. var bx1 = KeepContentAlwaysInViewport ? position * bw / Size : position * bw / (Size + bw);
  437. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / (Size + bw);
  438. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  439. bx1 = Math.Max (bx1 - 1, 0);
  440. }
  441. Move (0, row);
  442. Driver.AddRune (Driver.LeftArrow);
  443. bool hasLeftTee = false;
  444. bool hasDiamond = false;
  445. bool hasRightTee = false;
  446. for (int x = 0; x < bw; x++) {
  447. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  448. special = Driver.Stipple;
  449. } else {
  450. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  451. hasDiamond = true;
  452. special = Driver.Diamond;
  453. } else {
  454. if (x == bx1 && !hasLeftTee) {
  455. hasLeftTee = true;
  456. posLeftTee = x;
  457. special = Driver.LeftTee;
  458. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  459. hasRightTee = true;
  460. posRightTee = x;
  461. special = Driver.RightTee;
  462. } else {
  463. special = Driver.HLine;
  464. }
  465. }
  466. }
  467. Driver.AddRune (special);
  468. }
  469. if (!hasLeftTee) {
  470. Move (Bounds.Width - 2, row);
  471. Driver.AddRune (Driver.LeftTee);
  472. }
  473. Driver.AddRune (Driver.RightArrow);
  474. }
  475. }
  476. if (contentBottomRightCorner != null && hosted && showBothScrollIndicator) {
  477. contentBottomRightCorner.Redraw (contentBottomRightCorner.Bounds);
  478. }
  479. }
  480. int lastLocation = -1;
  481. int posBarOffset;
  482. ///<inheritdoc/>
  483. public override bool MouseEvent (MouseEvent me)
  484. {
  485. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1DoubleClicked &&
  486. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) &&
  487. me.Flags != MouseFlags.Button1Released && me.Flags != MouseFlags.WheeledDown &&
  488. me.Flags != MouseFlags.WheeledUp && me.Flags != MouseFlags.WheeledRight &&
  489. me.Flags != MouseFlags.WheeledLeft && me.Flags != MouseFlags.Button1TripleClicked) {
  490. return false;
  491. }
  492. if (Host != null && !Host.HasFocus) {
  493. Host.SetFocus ();
  494. }
  495. int location = vertical ? me.Y : me.X;
  496. int barsize = vertical ? Bounds.Height : Bounds.Width;
  497. int posTopLeftTee = vertical ? posTopTee + 1 : posLeftTee + 1;
  498. int posBottomRightTee = vertical ? posBottomTee + 1 : posRightTee + 1;
  499. barsize -= 2;
  500. var pos = Position;
  501. if (me.Flags != MouseFlags.Button1Released
  502. && (Application.mouseGrabView == null || Application.mouseGrabView != this)) {
  503. Application.GrabMouse (this);
  504. } else if (me.Flags == MouseFlags.Button1Released && Application.mouseGrabView != null && Application.mouseGrabView == this) {
  505. lastLocation = -1;
  506. Application.UngrabMouse ();
  507. return true;
  508. }
  509. if (showScrollIndicator && (me.Flags == MouseFlags.WheeledDown || me.Flags == MouseFlags.WheeledUp ||
  510. me.Flags == MouseFlags.WheeledRight || me.Flags == MouseFlags.WheeledLeft)) {
  511. return Host.MouseEvent (me);
  512. }
  513. if (location == 0) {
  514. if (pos > 0) {
  515. Position = pos - 1;
  516. }
  517. } else if (location == barsize + 1) {
  518. if (CanScroll (1, out _, vertical)) {
  519. Position = pos + 1;
  520. }
  521. } else if (location > 0 && location < barsize + 1) {
  522. //var b1 = pos * (Size > 0 ? barsize / Size : 0);
  523. //var b2 = Size > 0
  524. // ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  525. // : 0;
  526. //if (KeepContentAlwaysInViewport && b1 == b2) {
  527. // b1 = Math.Max (b1 - 1, 0);
  528. //}
  529. if (lastLocation > -1 || (location >= posTopLeftTee && location <= posBottomRightTee
  530. && me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  531. if (lastLocation == -1) {
  532. lastLocation = location;
  533. posBarOffset = keepContentAlwaysInViewport ? Math.Max (location - posTopLeftTee, 1) : 0;
  534. return true;
  535. }
  536. if (location > lastLocation) {
  537. if (location - posBarOffset < barsize) {
  538. var np = ((location - posBarOffset) * Size / barsize) + (Size / barsize);
  539. if (CanScroll (np - pos, out int nv, vertical)) {
  540. Position = pos + nv;
  541. }
  542. } else if (CanScroll (Size - pos, out int nv, vertical)) {
  543. Position = Math.Min (pos + nv, Size);
  544. }
  545. } else if (location < lastLocation) {
  546. if (location - posBarOffset > 0) {
  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 {
  552. Position = 0;
  553. }
  554. } else if (location - posBarOffset >= barsize && posBottomRightTee - posTopLeftTee >= 3 && CanScroll (Size - pos, out int nv, vertical)) {
  555. Position = Math.Min (pos + nv, Size);
  556. } else if (location - posBarOffset >= barsize - 1 && posBottomRightTee - posTopLeftTee <= 3 && CanScroll (Size - pos, out nv, vertical)) {
  557. Position = Math.Min (pos + nv, Size);
  558. } else if (location - posBarOffset <= 0 && posBottomRightTee - posTopLeftTee <= 3) {
  559. Position = 0;
  560. }
  561. } else if (location > posBottomRightTee) {
  562. if (CanScroll (barsize, out int nv, vertical)) {
  563. Position = pos + nv;
  564. }
  565. } else if (location < posTopLeftTee) {
  566. if (CanScroll (-barsize, out int nv, vertical)) {
  567. Position = pos + nv;
  568. }
  569. } else if (location == 1 && posTopLeftTee <= 3) {
  570. Position = 0;
  571. } else if (location == barsize) {
  572. if (CanScroll (Size - pos, out int nv, vertical)) {
  573. Position = Math.Min (pos + nv, Size);
  574. }
  575. }
  576. }
  577. return true;
  578. }
  579. internal bool CanScroll (int n, out int max, bool isVertical = false)
  580. {
  581. if (Host == null) {
  582. max = 0;
  583. return false;
  584. }
  585. int s = GetBarsize (isVertical);
  586. var newSize = Math.Max (Math.Min (size - s, position + n), 0);
  587. max = size > s + newSize ? (newSize == 0 ? -position : n) : size - (s + position) - 1;
  588. if (size >= s + newSize && max != 0) {
  589. return true;
  590. }
  591. return false;
  592. }
  593. int GetBarsize (bool isVertical)
  594. {
  595. if (Host == null) {
  596. return 0;
  597. }
  598. return isVertical ?
  599. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  600. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  601. }
  602. ///<inheritdoc/>
  603. public override bool OnEnter (View view)
  604. {
  605. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  606. return base.OnEnter (view);
  607. }
  608. }
  609. }