ScrollBarView.cs 21 KB

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