ScrollBarView.cs 23 KB

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