ScrollBarView.cs 22 KB

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