ScrollBarView.cs 25 KB

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