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