ScrollBarView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. }
  228. }
  229. }
  230. /// <summary>
  231. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  232. /// </summary>
  233. public bool AutoHideScrollBars {
  234. get => autoHideScrollBars;
  235. set {
  236. if (autoHideScrollBars != value) {
  237. autoHideScrollBars = value;
  238. SetNeedsDisplay ();
  239. }
  240. }
  241. }
  242. /// <summary>
  243. /// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
  244. /// </summary>
  245. public virtual void OnChangedPosition ()
  246. {
  247. ChangedPosition?.Invoke ();
  248. }
  249. /// <summary>
  250. /// Only used for a hosted view that will update and redraw the scrollbars.
  251. /// </summary>
  252. public virtual void Refresh ()
  253. {
  254. ShowHideScrollBars ();
  255. }
  256. void ShowHideScrollBars ()
  257. {
  258. if (!hosted || (hosted && !autoHideScrollBars)) {
  259. return;
  260. }
  261. var pending = CheckBothScrollBars (this);
  262. CheckBothScrollBars (otherScrollBarView, pending);
  263. SetWidthHeight ();
  264. SetRelativeLayout (Bounds);
  265. OtherScrollBarView.SetRelativeLayout (OtherScrollBarView.Bounds);
  266. if (showBothScrollIndicator) {
  267. contentBottomRightCorner.Visible = true;
  268. } else {
  269. contentBottomRightCorner.Visible = false;
  270. }
  271. if (showScrollIndicator) {
  272. Redraw (Bounds);
  273. }
  274. if (otherScrollBarView.showScrollIndicator) {
  275. otherScrollBarView.Redraw (otherScrollBarView.Bounds);
  276. }
  277. }
  278. bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  279. {
  280. int barsize = scrollBarView.vertical ? scrollBarView.Bounds.Height : scrollBarView.Bounds.Width;
  281. if (barsize == 0 || barsize > scrollBarView.size) {
  282. if (scrollBarView.showScrollIndicator) {
  283. scrollBarView.ShowScrollIndicator = false;
  284. }
  285. } else if (barsize > 0 && barsize == scrollBarView.size && scrollBarView.OtherScrollBarView != null && pending) {
  286. if (scrollBarView.showScrollIndicator) {
  287. scrollBarView.ShowScrollIndicator = false;
  288. }
  289. if (scrollBarView.OtherScrollBarView != null && scrollBarView.showBothScrollIndicator) {
  290. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  291. }
  292. } else if (barsize > 0 && barsize == size && scrollBarView.OtherScrollBarView != null && !pending) {
  293. pending = true;
  294. } else {
  295. if (scrollBarView.OtherScrollBarView != null && pending) {
  296. if (!scrollBarView.showBothScrollIndicator) {
  297. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  298. }
  299. }
  300. if (!scrollBarView.showScrollIndicator) {
  301. scrollBarView.ShowScrollIndicator = true;
  302. }
  303. }
  304. return pending;
  305. }
  306. void SetWidthHeight ()
  307. {
  308. if (showBothScrollIndicator) {
  309. Width = vertical ? 1 : Dim.Width (Host) - 1;
  310. Height = vertical ? Dim.Height (Host) - 1 : 1;
  311. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 1;
  312. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 1 : 1;
  313. } else if (showScrollIndicator) {
  314. Width = vertical ? 1 : Dim.Width (Host) - 0;
  315. Height = vertical ? Dim.Height (Host) - 0 : 1;
  316. } else if (otherScrollBarView != null && otherScrollBarView.showScrollIndicator) {
  317. otherScrollBarView.Width = otherScrollBarView.vertical ? 1 : Dim.Width (Host) - 0;
  318. otherScrollBarView.Height = otherScrollBarView.vertical ? Dim.Height (Host) - 0 : 1;
  319. }
  320. }
  321. int posTopTee;
  322. int posLeftTee;
  323. int posBottomTee;
  324. int posRightTee;
  325. ///<inheritdoc/>
  326. public override void Redraw (Rect region)
  327. {
  328. if (ColorScheme == null || Size == 0) {
  329. return;
  330. }
  331. Driver.SetAttribute (ColorScheme.Normal);
  332. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  333. return;
  334. }
  335. if (vertical) {
  336. if (region.Right < Bounds.Width - 1) {
  337. return;
  338. }
  339. var col = Bounds.Width - 1;
  340. var bh = Bounds.Height;
  341. Rune special;
  342. if (bh < 4) {
  343. var by1 = position * bh / Size;
  344. var by2 = (position + bh) * bh / Size;
  345. Move (col, 0);
  346. if (Bounds.Height == 1) {
  347. Driver.AddRune (Driver.Diamond);
  348. } else {
  349. Driver.AddRune (Driver.UpArrow);
  350. }
  351. if (Bounds.Height == 3) {
  352. Move (col, 1);
  353. Driver.AddRune (Driver.Diamond);
  354. }
  355. if (Bounds.Height > 1) {
  356. Move (col, Bounds.Height - 1);
  357. Driver.AddRune (Driver.DownArrow);
  358. }
  359. } else {
  360. bh -= 2;
  361. var by1 = position * bh / Size;
  362. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / Size;
  363. if (KeepContentAlwaysInViewport && by1 == by2) {
  364. by1 = Math.Max (by1 - 1, 0);
  365. }
  366. Move (col, 0);
  367. Driver.AddRune (Driver.UpArrow);
  368. Move (col, Bounds.Height - 1);
  369. Driver.AddRune (Driver.DownArrow);
  370. bool hasTopTee = false;
  371. bool hasDiamond = false;
  372. bool hasBottomTee = false;
  373. for (int y = 0; y < bh; y++) {
  374. Move (col, y + 1);
  375. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  376. special = Driver.Stipple;
  377. } else {
  378. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  379. hasDiamond = true;
  380. special = Driver.Diamond;
  381. } else {
  382. if (y == by1 && !hasTopTee) {
  383. hasTopTee = true;
  384. posTopTee = y;
  385. special = Driver.TopTee;
  386. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  387. hasBottomTee = true;
  388. posBottomTee = y;
  389. special = Driver.BottomTee;
  390. } else {
  391. special = Driver.VLine;
  392. }
  393. }
  394. }
  395. Driver.AddRune (special);
  396. }
  397. if (!hasTopTee) {
  398. Move (col, Bounds.Height - 2);
  399. Driver.AddRune (Driver.TopTee);
  400. }
  401. }
  402. } else {
  403. if (region.Bottom < Bounds.Height - 1) {
  404. return;
  405. }
  406. var row = Bounds.Height - 1;
  407. var bw = Bounds.Width;
  408. Rune special;
  409. if (bw < 4) {
  410. var bx1 = position * bw / Size;
  411. var bx2 = (position + bw) * bw / Size;
  412. Move (0, row);
  413. Driver.AddRune (Driver.LeftArrow);
  414. Driver.AddRune (Driver.RightArrow);
  415. } else {
  416. bw -= 2;
  417. var bx1 = position * bw / Size;
  418. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / Size;
  419. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  420. bx1 = Math.Max (bx1 - 1, 0);
  421. }
  422. Move (0, row);
  423. Driver.AddRune (Driver.LeftArrow);
  424. bool hasLeftTee = false;
  425. bool hasDiamond = false;
  426. bool hasRightTee = false;
  427. for (int x = 0; x < bw; x++) {
  428. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  429. special = Driver.Stipple;
  430. } else {
  431. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  432. hasDiamond = true;
  433. special = Driver.Diamond;
  434. } else {
  435. if (x == bx1 && !hasLeftTee) {
  436. hasLeftTee = true;
  437. posLeftTee = x;
  438. special = Driver.LeftTee;
  439. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  440. hasRightTee = true;
  441. posRightTee = x;
  442. special = Driver.RightTee;
  443. } else {
  444. special = Driver.HLine;
  445. }
  446. }
  447. }
  448. Driver.AddRune (special);
  449. }
  450. if (!hasLeftTee) {
  451. Move (Bounds.Width - 2, row);
  452. Driver.AddRune (Driver.LeftTee);
  453. }
  454. Driver.AddRune (Driver.RightArrow);
  455. }
  456. }
  457. if (hosted && showBothScrollIndicator) {
  458. contentBottomRightCorner.Redraw (contentBottomRightCorner.Bounds);
  459. }
  460. }
  461. int lastLocation = -1;
  462. ///<inheritdoc/>
  463. public override bool MouseEvent (MouseEvent me)
  464. {
  465. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  466. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) &&
  467. me.Flags != MouseFlags.Button1Released && me.Flags != MouseFlags.WheeledDown &&
  468. me.Flags != MouseFlags.WheeledUp && me.Flags != MouseFlags.WheeledRight &&
  469. me.Flags != MouseFlags.WheeledLeft) {
  470. return false;
  471. }
  472. if (Host != null && !Host.HasFocus) {
  473. Host.SetFocus ();
  474. }
  475. int location = vertical ? me.Y : me.X;
  476. int barsize = vertical ? Bounds.Height : Bounds.Width;
  477. int posTopLeftTee = vertical ? posTopTee + 1 : posLeftTee + 1;
  478. int posBottomRightTee = vertical ? posBottomTee + 1 : posRightTee + 1;
  479. barsize -= 2;
  480. var pos = Position;
  481. if ((me.Flags.HasFlag (MouseFlags.Button1Pressed) ||
  482. me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  483. && (Application.mouseGrabView == null || Application.mouseGrabView != this)) {
  484. Application.GrabMouse (this);
  485. } else if (me.Flags == MouseFlags.Button1Released && Application.mouseGrabView != null && Application.mouseGrabView == this) {
  486. lastLocation = -1;
  487. Application.UngrabMouse ();
  488. return true;
  489. } else if (showScrollIndicator && (me.Flags == MouseFlags.WheeledDown || me.Flags == MouseFlags.WheeledUp ||
  490. me.Flags == MouseFlags.WheeledRight || me.Flags == MouseFlags.WheeledLeft)) {
  491. return Host.MouseEvent (me);
  492. }
  493. if (location == 0) {
  494. if (pos > 0) {
  495. Position = pos - 1;
  496. }
  497. } else if (location == barsize + 1) {
  498. if (CanScroll (1, out _, vertical)) {
  499. Position = pos + 1;
  500. }
  501. } else if (location > 0 && location < barsize + 1) {
  502. var b1 = pos * (Size > 0 ? barsize / Size : 0);
  503. var b2 = Size > 0
  504. ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  505. : 0;
  506. if (KeepContentAlwaysInViewport && b1 == b2) {
  507. b1 = Math.Max (b1 - 1, 0);
  508. }
  509. if (lastLocation == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1Clicked) {
  510. if (location == 1) {
  511. if (location < posTopLeftTee && CanScroll (-barsize, out int nv, vertical)) {
  512. Position = pos + nv;
  513. }
  514. if (location == posTopLeftTee) {
  515. Position = 0;
  516. }
  517. } else if (location == barsize) {
  518. if (location > posBottomRightTee && CanScroll (barsize, out int nv, vertical)) {
  519. Position = pos + nv;
  520. }
  521. if (location == posBottomRightTee && CanScroll (Size - pos, out nv, vertical)) {
  522. Position = Math.Min (pos + nv, Size);
  523. }
  524. } else if (location < posTopLeftTee) {
  525. if (CanScroll (-barsize, out int nv, vertical)) {
  526. Position = pos + nv;
  527. }
  528. } else if (location > posBottomRightTee) {
  529. if (CanScroll (barsize, out int nv, vertical)) {
  530. Position = pos + nv;
  531. }
  532. }
  533. } else if (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  534. var posBarLength = posBottomRightTee - posTopLeftTee;
  535. if (lastLocation > -1 ||
  536. location > 1 && location < barsize && location >= posTopLeftTee && location <= posBottomRightTee) {
  537. if (lastLocation == -1) {
  538. lastLocation = location;
  539. return true;
  540. }
  541. var np = location * Size / barsize - lastLocation;
  542. if (CanScroll (np - pos, out int nv, vertical)) {
  543. Position = pos + nv;
  544. }
  545. if (location > lastLocation && location == barsize) {
  546. if (CanScroll (Size - pos, out nv, vertical)) {
  547. Position = Math.Min (pos + nv, Size);
  548. }
  549. } else if (location < lastLocation && location == 1 && b2 <= 2) {
  550. Position = 0;
  551. }
  552. } else if (location > posBottomRightTee) {
  553. if (CanScroll (barsize, out int nv, vertical)) {
  554. Position = pos + nv;
  555. }
  556. } else if (location < posTopLeftTee) {
  557. if (CanScroll (-barsize, out int nv, vertical)) {
  558. Position = pos + nv;
  559. }
  560. } else if (location == 1 && b2 <= 2) {
  561. Position = 0;
  562. } else if (location == barsize) {
  563. if (CanScroll (Size - pos, out int nv, vertical)) {
  564. Position = Math.Min (pos + nv, Size);
  565. }
  566. }
  567. }
  568. }
  569. return true;
  570. }
  571. internal bool CanScroll (int n, out int max, bool isVertical = false)
  572. {
  573. if (Host == null) {
  574. max = 0;
  575. return false;
  576. }
  577. var s = isVertical ?
  578. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  579. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  580. var newSize = Math.Max (Math.Min (size - s, position + n), 0);
  581. max = size > s + newSize ? (newSize == 0 ? -position : n) : size - (s + position) - 1;
  582. if (size >= s + newSize && max != 0) {
  583. return true;
  584. }
  585. return false;
  586. }
  587. }
  588. }