2
0

ScrollBarView.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System.Diagnostics;
  8. namespace Terminal.Gui;
  9. /// <summary>ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical</summary>
  10. /// <remarks>
  11. /// <para>
  12. /// The scrollbar is drawn to be a representation of the Size, assuming that the scroll position is set at
  13. /// Position.
  14. /// </para>
  15. /// <para>If the region to display the scrollbar is larger than three characters, arrow indicators are drawn.</para>
  16. /// </remarks>
  17. public class ScrollBarView : View
  18. {
  19. private bool _autoHideScrollBars = true;
  20. private View _contentBottomRightCorner;
  21. private bool _hosted;
  22. private bool _keepContentAlwaysInViewport = true;
  23. private int _lastLocation = -1;
  24. private ScrollBarView _otherScrollBarView;
  25. private int _posBarOffset;
  26. private int _posBottomTee;
  27. private int _posLeftTee;
  28. private int _posRightTee;
  29. private int _posTopTee;
  30. private bool _showScrollIndicator;
  31. private int _size, _position;
  32. private bool _vertical;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class.
  35. /// </summary>
  36. public ScrollBarView ()
  37. {
  38. WantContinuousButtonPressed = true;
  39. Added += (s, e) => CreateBottomRightCorner (e.SuperView);
  40. Initialized += ScrollBarView_Initialized;
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class.
  44. /// </summary>
  45. /// <param name="host">The view that will host this scrollbar.</param>
  46. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  47. /// <param name="showBothScrollIndicator">
  48. /// If set to <c>true (default)</c> will have the other scrollbar, otherwise will
  49. /// have only one.
  50. /// </param>
  51. public ScrollBarView (View host, bool isVertical, bool showBothScrollIndicator = true)
  52. {
  53. if (host is null)
  54. {
  55. throw new ArgumentNullException ("The host parameter can't be null.");
  56. }
  57. if (host.SuperView is null)
  58. {
  59. throw new ArgumentNullException ("The host SuperView parameter can't be null.");
  60. }
  61. _hosted = true;
  62. IsVertical = isVertical;
  63. ColorScheme = host.ColorScheme;
  64. X = isVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  65. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  66. Host = host;
  67. CanFocus = false;
  68. Enabled = host.Enabled;
  69. Visible = host.Visible;
  70. Initialized += ScrollBarView_Initialized;
  71. //Host.CanFocusChanged += Host_CanFocusChanged;
  72. Host.EnabledChanged += Host_EnabledChanged;
  73. Host.VisibleChanged += Host_VisibleChanged;
  74. Host.SuperView.Add (this);
  75. AutoHideScrollBars = true;
  76. if (showBothScrollIndicator)
  77. {
  78. OtherScrollBarView = new ScrollBarView
  79. {
  80. IsVertical = !isVertical,
  81. ColorScheme = host.ColorScheme,
  82. Host = host,
  83. CanFocus = false,
  84. Enabled = host.Enabled,
  85. Visible = host.Visible,
  86. OtherScrollBarView = this
  87. };
  88. OtherScrollBarView._hosted = true;
  89. OtherScrollBarView.X = OtherScrollBarView.IsVertical ? Pos.Right (host) - 1 : Pos.Left (host);
  90. OtherScrollBarView.Y = OtherScrollBarView.IsVertical ? Pos.Top (host) : Pos.Bottom (host) - 1;
  91. OtherScrollBarView.Host.SuperView.Add (OtherScrollBarView);
  92. OtherScrollBarView.ShowScrollIndicator = true;
  93. }
  94. ShowScrollIndicator = true;
  95. CreateBottomRightCorner (Host);
  96. }
  97. /// <summary>If true the vertical/horizontal scroll bars won't be showed if it's not needed.</summary>
  98. public bool AutoHideScrollBars
  99. {
  100. get => _autoHideScrollBars;
  101. set
  102. {
  103. if (_autoHideScrollBars != value)
  104. {
  105. _autoHideScrollBars = value;
  106. SetNeedsDisplay ();
  107. }
  108. }
  109. }
  110. // BUGBUG: v2 - for consistency this should be named "Parent" not "Host"
  111. /// <summary>Get or sets the view that host this <see cref="ScrollBarView"/></summary>
  112. public View Host { get; internal set; }
  113. /// <summary>If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</summary>
  114. public bool IsVertical
  115. {
  116. get => _vertical;
  117. set
  118. {
  119. _vertical = value;
  120. if (IsInitialized)
  121. {
  122. SetWidthHeight ();
  123. }
  124. }
  125. }
  126. /// <summary>Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/></summary>
  127. public bool KeepContentAlwaysInViewport
  128. {
  129. get => _keepContentAlwaysInViewport;
  130. set
  131. {
  132. if (_keepContentAlwaysInViewport != value)
  133. {
  134. _keepContentAlwaysInViewport = value;
  135. var pos = 0;
  136. if (value && !_vertical && _position + Host.Viewport.Width > _size)
  137. {
  138. pos = _size - Host.Viewport.Width + (_showBothScrollIndicator ? 1 : 0);
  139. }
  140. if (value && _vertical && _position + Host.Viewport.Height > _size)
  141. {
  142. pos = _size - Host.Viewport.Height + (_showBothScrollIndicator ? 1 : 0);
  143. }
  144. if (pos != 0)
  145. {
  146. Position = pos;
  147. }
  148. if (OtherScrollBarView is { } && OtherScrollBarView._keepContentAlwaysInViewport != value)
  149. {
  150. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  151. }
  152. if (pos == 0)
  153. {
  154. Refresh ();
  155. }
  156. }
  157. }
  158. }
  159. /// <summary>Represent a vertical or horizontal ScrollBarView other than this.</summary>
  160. public ScrollBarView OtherScrollBarView
  161. {
  162. get => _otherScrollBarView;
  163. set
  164. {
  165. if (value is { } && ((value.IsVertical && _vertical) || (!value.IsVertical && !_vertical)))
  166. {
  167. throw new ArgumentException (
  168. $"There is already a {(_vertical ? "vertical" : "horizontal")} ScrollBarView."
  169. );
  170. }
  171. _otherScrollBarView = value;
  172. }
  173. }
  174. /// <summary>The position, relative to <see cref="Size"/>, to set the scrollbar at.</summary>
  175. /// <value>The position.</value>
  176. public int Position
  177. {
  178. get => _position;
  179. set
  180. {
  181. if (_position == value)
  182. {
  183. return;
  184. }
  185. SetPosition (value);
  186. }
  187. }
  188. // BUGBUG: v2 - Why can't we get rid of this and just use Visible?
  189. /// <summary>Gets or sets the visibility for the vertical or horizontal scroll indicator.</summary>
  190. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  191. public bool ShowScrollIndicator
  192. {
  193. get => _showScrollIndicator && Visible;
  194. set
  195. {
  196. //if (value == showScrollIndicator) {
  197. // return;
  198. //}
  199. _showScrollIndicator = value;
  200. if (IsInitialized)
  201. {
  202. SetLayoutNeeded ();
  203. if (value)
  204. {
  205. Visible = true;
  206. }
  207. else
  208. {
  209. Visible = false;
  210. Position = 0;
  211. }
  212. SetWidthHeight ();
  213. }
  214. }
  215. }
  216. /// <summary>The size of content the scrollbar represents.</summary>
  217. /// <value>The size.</value>
  218. /// <remarks>
  219. /// The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is part of a
  220. /// <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.
  221. /// </remarks>
  222. public int Size
  223. {
  224. get => _size;
  225. set
  226. {
  227. _size = value;
  228. if (IsInitialized)
  229. {
  230. SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  231. ShowHideScrollBars (false);
  232. SetLayoutNeeded ();
  233. }
  234. }
  235. }
  236. private bool _showBothScrollIndicator => OtherScrollBarView?.ShowScrollIndicator == true && ShowScrollIndicator;
  237. /// <summary>This event is raised when the position on the scrollbar has changed.</summary>
  238. public event EventHandler ChangedPosition;
  239. /// <inheritdoc/>
  240. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  241. {
  242. if (mouseEvent.Flags != MouseFlags.Button1Pressed
  243. && mouseEvent.Flags != MouseFlags.Button1DoubleClicked
  244. && !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)
  245. && mouseEvent.Flags != MouseFlags.Button1Released
  246. && mouseEvent.Flags != MouseFlags.WheeledDown
  247. && mouseEvent.Flags != MouseFlags.WheeledUp
  248. && mouseEvent.Flags != MouseFlags.WheeledRight
  249. && mouseEvent.Flags != MouseFlags.WheeledLeft
  250. && mouseEvent.Flags != MouseFlags.Button1TripleClicked)
  251. {
  252. return false;
  253. }
  254. if (!Host.CanFocus)
  255. {
  256. return true;
  257. }
  258. if (Host?.HasFocus == false)
  259. {
  260. Host.SetFocus ();
  261. }
  262. int location = _vertical ? mouseEvent.Position.Y : mouseEvent.Position.X;
  263. int barsize = _vertical ? Viewport.Height : Viewport.Width;
  264. int posTopLeftTee = _vertical ? _posTopTee + 1 : _posLeftTee + 1;
  265. int posBottomRightTee = _vertical ? _posBottomTee + 1 : _posRightTee + 1;
  266. barsize -= 2;
  267. int pos = Position;
  268. if (mouseEvent.Flags != MouseFlags.Button1Released && (Application.MouseGrabView is null || Application.MouseGrabView != this))
  269. {
  270. Application.GrabMouse (this);
  271. }
  272. else if (mouseEvent.Flags == MouseFlags.Button1Released && Application.MouseGrabView is { } && Application.MouseGrabView == this)
  273. {
  274. _lastLocation = -1;
  275. Application.UngrabMouse ();
  276. return true;
  277. }
  278. if (ShowScrollIndicator
  279. && (mouseEvent.Flags == MouseFlags.WheeledDown
  280. || mouseEvent.Flags == MouseFlags.WheeledUp
  281. || mouseEvent.Flags == MouseFlags.WheeledRight
  282. || mouseEvent.Flags == MouseFlags.WheeledLeft))
  283. {
  284. return Host.NewMouseEvent (mouseEvent) == true;
  285. }
  286. if (mouseEvent.Flags == MouseFlags.Button1Pressed && location == 0)
  287. {
  288. if (pos > 0)
  289. {
  290. Position = pos - 1;
  291. }
  292. }
  293. else if (mouseEvent.Flags == MouseFlags.Button1Pressed && location == barsize + 1)
  294. {
  295. if (CanScroll (1, out _, _vertical))
  296. {
  297. Position = pos + 1;
  298. }
  299. }
  300. else if (location > 0 && location < barsize + 1)
  301. {
  302. //var b1 = pos * (Size > 0 ? barsize / Size : 0);
  303. //var b2 = Size > 0
  304. // ? (KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size)
  305. // : 0;
  306. //if (KeepContentAlwaysInViewport && b1 == b2) {
  307. // b1 = Math.Max (b1 - 1, 0);
  308. //}
  309. if (_lastLocation > -1
  310. || (location >= posTopLeftTee
  311. && location <= posBottomRightTee
  312. && mouseEvent.Flags.HasFlag (
  313. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  314. )))
  315. {
  316. if (_lastLocation == -1)
  317. {
  318. _lastLocation = location;
  319. _posBarOffset = _keepContentAlwaysInViewport
  320. ? Math.Max (location - posTopLeftTee, 1)
  321. : 0;
  322. return true;
  323. }
  324. if (location > _lastLocation)
  325. {
  326. if (location - _posBarOffset < barsize)
  327. {
  328. int np = (location - _posBarOffset) * Size / barsize + Size / barsize;
  329. if (CanScroll (np - pos, out int nv, _vertical))
  330. {
  331. Position = pos + nv;
  332. }
  333. }
  334. else if (CanScroll (Size - pos, out int nv, _vertical))
  335. {
  336. Position = Math.Min (pos + nv, Size);
  337. }
  338. }
  339. else if (location < _lastLocation)
  340. {
  341. if (location - _posBarOffset > 0)
  342. {
  343. int np = (location - _posBarOffset) * Size / barsize - Size / barsize;
  344. if (CanScroll (np - pos, out int nv, _vertical))
  345. {
  346. Position = pos + nv;
  347. }
  348. }
  349. else
  350. {
  351. Position = 0;
  352. }
  353. }
  354. else if (location - _posBarOffset >= barsize && posBottomRightTee - posTopLeftTee >= 3 && CanScroll (Size - pos, out int nv, _vertical))
  355. {
  356. Position = Math.Min (pos + nv, Size);
  357. }
  358. else if (location - _posBarOffset >= barsize - 1 && posBottomRightTee - posTopLeftTee <= 3 && CanScroll (Size - pos, out nv, _vertical))
  359. {
  360. Position = Math.Min (pos + nv, Size);
  361. }
  362. else if (location - _posBarOffset <= 0 && posBottomRightTee - posTopLeftTee <= 3)
  363. {
  364. Position = 0;
  365. }
  366. }
  367. else if (location > posBottomRightTee)
  368. {
  369. if (CanScroll (barsize, out int nv, _vertical))
  370. {
  371. Position = pos + nv;
  372. }
  373. }
  374. else if (location < posTopLeftTee)
  375. {
  376. if (CanScroll (-barsize, out int nv, _vertical))
  377. {
  378. Position = pos + nv;
  379. }
  380. }
  381. else if (location == 1 && posTopLeftTee <= 3)
  382. {
  383. Position = 0;
  384. }
  385. else if (location == barsize)
  386. {
  387. if (CanScroll (Size - pos, out int nv, _vertical))
  388. {
  389. Position = Math.Min (pos + nv, Size);
  390. }
  391. }
  392. }
  393. return true;
  394. }
  395. /// <summary>Virtual method to invoke the <see cref="ChangedPosition"/> action event.</summary>
  396. public virtual void OnChangedPosition () { ChangedPosition?.Invoke (this, EventArgs.Empty); }
  397. /// <inheritdoc/>
  398. public override void OnDrawContent (Rectangle viewport)
  399. {
  400. if (ColorScheme is null || ((!ShowScrollIndicator || Size == 0) && AutoHideScrollBars && Visible))
  401. {
  402. if ((!ShowScrollIndicator || Size == 0) && AutoHideScrollBars && Visible)
  403. {
  404. ShowHideScrollBars (false);
  405. }
  406. return;
  407. }
  408. if (Size == 0 || (_vertical && Viewport.Height == 0) || (!_vertical && Viewport.Width == 0))
  409. {
  410. return;
  411. }
  412. Driver.SetAttribute (Host.HasFocus ? ColorScheme.Focus : GetNormalColor ());
  413. if (_vertical)
  414. {
  415. if (Viewport.Right < Viewport.Width - 1)
  416. {
  417. return;
  418. }
  419. int col = Viewport.Width - 1;
  420. int bh = Viewport.Height;
  421. Rune special;
  422. if (bh < 4)
  423. {
  424. int by1 = _position * bh / Size;
  425. int by2 = (_position + bh) * bh / Size;
  426. Move (col, 0);
  427. if (Viewport.Height == 1)
  428. {
  429. Driver.AddRune (Glyphs.Diamond);
  430. }
  431. else
  432. {
  433. Driver.AddRune (Glyphs.UpArrow);
  434. }
  435. if (Viewport.Height == 3)
  436. {
  437. Move (col, 1);
  438. Driver.AddRune (Glyphs.Diamond);
  439. }
  440. if (Viewport.Height > 1)
  441. {
  442. Move (col, Viewport.Height - 1);
  443. Driver.AddRune (Glyphs.DownArrow);
  444. }
  445. }
  446. else
  447. {
  448. bh -= 2;
  449. int by1 = KeepContentAlwaysInViewport
  450. ? _position * bh / Size
  451. : _position * bh / (Size + bh);
  452. int by2 = KeepContentAlwaysInViewport
  453. ? Math.Min ((_position + bh) * bh / Size + 1, bh - 1)
  454. : (_position + bh) * bh / (Size + bh);
  455. if (KeepContentAlwaysInViewport && by1 == by2)
  456. {
  457. by1 = Math.Max (by1 - 1, 0);
  458. }
  459. AddRune (col, 0, Glyphs.UpArrow);
  460. var hasTopTee = false;
  461. var hasDiamond = false;
  462. var hasBottomTee = false;
  463. for (var y = 0; y < bh; y++)
  464. {
  465. if ((y < by1 || y > by2) && ((_position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee)))
  466. {
  467. special = Glyphs.Stipple;
  468. }
  469. else
  470. {
  471. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond)
  472. {
  473. hasDiamond = true;
  474. special = Glyphs.Diamond;
  475. }
  476. else
  477. {
  478. if (y == by1 && !hasTopTee)
  479. {
  480. hasTopTee = true;
  481. _posTopTee = y;
  482. special = Glyphs.TopTee;
  483. }
  484. else if (((_position == 0 && y == bh - 1) || y >= by2 || by2 == 0) && !hasBottomTee)
  485. {
  486. hasBottomTee = true;
  487. _posBottomTee = y;
  488. special = Glyphs.BottomTee;
  489. }
  490. else
  491. {
  492. special = Glyphs.VLine;
  493. }
  494. }
  495. }
  496. AddRune (col, y + 1, special);
  497. }
  498. if (!hasTopTee)
  499. {
  500. AddRune (col, Viewport.Height - 2, Glyphs.TopTee);
  501. }
  502. AddRune (col, Viewport.Height - 1, Glyphs.DownArrow);
  503. }
  504. }
  505. else
  506. {
  507. if (Viewport.Bottom < Viewport.Height - 1)
  508. {
  509. return;
  510. }
  511. int row = Viewport.Height - 1;
  512. int bw = Viewport.Width;
  513. Rune special;
  514. if (bw < 4)
  515. {
  516. int bx1 = _position * bw / Size;
  517. int bx2 = (_position + bw) * bw / Size;
  518. Move (0, row);
  519. Driver.AddRune (Glyphs.LeftArrow);
  520. Driver.AddRune (Glyphs.RightArrow);
  521. }
  522. else
  523. {
  524. bw -= 2;
  525. int bx1 = KeepContentAlwaysInViewport
  526. ? _position * bw / Size
  527. : _position * bw / (Size + bw);
  528. int bx2 = KeepContentAlwaysInViewport
  529. ? Math.Min ((_position + bw) * bw / Size + 1, bw - 1)
  530. : (_position + bw) * bw / (Size + bw);
  531. if (KeepContentAlwaysInViewport && bx1 == bx2)
  532. {
  533. bx1 = Math.Max (bx1 - 1, 0);
  534. }
  535. Move (0, row);
  536. Driver.AddRune (Glyphs.LeftArrow);
  537. var hasLeftTee = false;
  538. var hasDiamond = false;
  539. var hasRightTee = false;
  540. for (var x = 0; x < bw; x++)
  541. {
  542. if ((x < bx1 || x >= bx2 + 1) && ((_position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee)))
  543. {
  544. special = Glyphs.Stipple;
  545. }
  546. else
  547. {
  548. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond)
  549. {
  550. hasDiamond = true;
  551. special = Glyphs.Diamond;
  552. }
  553. else
  554. {
  555. if (x == bx1 && !hasLeftTee)
  556. {
  557. hasLeftTee = true;
  558. _posLeftTee = x;
  559. special = Glyphs.LeftTee;
  560. }
  561. else if (((_position == 0 && x == bw - 1) || x >= bx2 || bx2 == 0) && !hasRightTee)
  562. {
  563. hasRightTee = true;
  564. _posRightTee = x;
  565. special = Glyphs.RightTee;
  566. }
  567. else
  568. {
  569. special = Glyphs.HLine;
  570. }
  571. }
  572. }
  573. Driver.AddRune (special);
  574. }
  575. if (!hasLeftTee)
  576. {
  577. Move (Viewport.Width - 2, row);
  578. Driver.AddRune (Glyphs.LeftTee);
  579. }
  580. Driver.AddRune (Glyphs.RightArrow);
  581. }
  582. }
  583. }
  584. /// <summary>Only used for a hosted view that will update and redraw the scrollbars.</summary>
  585. public virtual void Refresh () { ShowHideScrollBars (); }
  586. internal bool CanScroll (int n, out int max, bool isVertical = false)
  587. {
  588. if (Host?.Viewport.IsEmpty != false)
  589. {
  590. max = 0;
  591. return false;
  592. }
  593. int s = GetBarsize (isVertical);
  594. int newSize = Math.Max (Math.Min (_size - s, _position + n), 0);
  595. max = _size > s + newSize ? newSize == 0 ? -_position : n : _size - (s + _position) - 1;
  596. if (_size >= s + newSize && max != 0)
  597. {
  598. return true;
  599. }
  600. return false;
  601. }
  602. private bool CheckBothScrollBars (ScrollBarView scrollBarView, bool pending = false)
  603. {
  604. int barsize = scrollBarView._vertical ? scrollBarView.Viewport.Height : scrollBarView.Viewport.Width;
  605. if (barsize == 0 || barsize >= scrollBarView._size)
  606. {
  607. if (scrollBarView.ShowScrollIndicator)
  608. {
  609. scrollBarView.ShowScrollIndicator = false;
  610. }
  611. if (scrollBarView.Visible)
  612. {
  613. scrollBarView.Visible = false;
  614. }
  615. }
  616. else if (barsize > 0 && barsize == scrollBarView._size && scrollBarView.OtherScrollBarView is { } && pending)
  617. {
  618. if (scrollBarView.ShowScrollIndicator)
  619. {
  620. scrollBarView.ShowScrollIndicator = false;
  621. }
  622. if (scrollBarView.Visible)
  623. {
  624. scrollBarView.Visible = false;
  625. }
  626. if (scrollBarView.OtherScrollBarView is { } && scrollBarView._showBothScrollIndicator)
  627. {
  628. scrollBarView.OtherScrollBarView.ShowScrollIndicator = false;
  629. }
  630. if (scrollBarView.OtherScrollBarView.Visible)
  631. {
  632. scrollBarView.OtherScrollBarView.Visible = false;
  633. }
  634. }
  635. else if (barsize > 0 && barsize == _size && scrollBarView.OtherScrollBarView is { } && !pending)
  636. {
  637. pending = true;
  638. }
  639. else
  640. {
  641. if (scrollBarView.OtherScrollBarView is { } && pending)
  642. {
  643. if (!scrollBarView._showBothScrollIndicator)
  644. {
  645. scrollBarView.OtherScrollBarView.ShowScrollIndicator = true;
  646. }
  647. if (!scrollBarView.OtherScrollBarView.Visible)
  648. {
  649. scrollBarView.OtherScrollBarView.Visible = true;
  650. }
  651. }
  652. if (!scrollBarView.ShowScrollIndicator)
  653. {
  654. scrollBarView.ShowScrollIndicator = true;
  655. }
  656. if (!scrollBarView.Visible)
  657. {
  658. scrollBarView.Visible = true;
  659. }
  660. }
  661. return pending;
  662. }
  663. private void ContentBottomRightCorner_DrawContent (object sender, DrawEventArgs e)
  664. {
  665. Driver.SetAttribute (Host.HasFocus ? ColorScheme.Focus : GetNormalColor ());
  666. // I'm forced to do this here because the Clear method is
  667. // changing the color attribute and is different of this one
  668. Driver.FillRect (Driver.Clip);
  669. e.Cancel = true;
  670. }
  671. //private void Host_CanFocusChanged ()
  672. //{
  673. // CanFocus = Host.CanFocus;
  674. // if (otherScrollBarView is { }) {
  675. // otherScrollBarView.CanFocus = CanFocus;
  676. // }
  677. //}
  678. private void ContentBottomRightCorner_MouseClick (object sender, MouseEventArgs me)
  679. {
  680. if (me.Flags == MouseFlags.WheeledDown
  681. || me.Flags == MouseFlags.WheeledUp
  682. || me.Flags == MouseFlags.WheeledRight
  683. || me.Flags == MouseFlags.WheeledLeft)
  684. {
  685. NewMouseEvent (me);
  686. }
  687. else if (me.Flags == MouseFlags.Button1Clicked)
  688. {
  689. Host.SetFocus ();
  690. }
  691. me.Handled = true;
  692. }
  693. private void CreateBottomRightCorner (View host)
  694. {
  695. if (Host is null)
  696. {
  697. Host = host;
  698. }
  699. if (Host != null
  700. && ((_contentBottomRightCorner is null && OtherScrollBarView is null)
  701. || (_contentBottomRightCorner is null && OtherScrollBarView is { } && OtherScrollBarView._contentBottomRightCorner is null)))
  702. {
  703. _contentBottomRightCorner = new ContentBottomRightCorner { Visible = Host.Visible };
  704. if (_hosted)
  705. {
  706. Host.SuperView.Add (_contentBottomRightCorner);
  707. _contentBottomRightCorner.X = Pos.Right (Host) - 1;
  708. _contentBottomRightCorner.Y = Pos.Bottom (Host) - 1;
  709. }
  710. else
  711. {
  712. Host.Add (_contentBottomRightCorner);
  713. _contentBottomRightCorner.X = Pos.AnchorEnd (1);
  714. _contentBottomRightCorner.Y = Pos.AnchorEnd (1);
  715. }
  716. _contentBottomRightCorner.Width = 1;
  717. _contentBottomRightCorner.Height = 1;
  718. _contentBottomRightCorner.MouseClick += ContentBottomRightCorner_MouseClick;
  719. _contentBottomRightCorner.DrawContent += ContentBottomRightCorner_DrawContent;
  720. }
  721. }
  722. private int GetBarsize (bool isVertical)
  723. {
  724. if (Host?.Viewport.IsEmpty != false)
  725. {
  726. return 0;
  727. }
  728. return isVertical ? KeepContentAlwaysInViewport
  729. ? Host.Viewport.Height + (_showBothScrollIndicator ? -2 : -1)
  730. : 0 :
  731. KeepContentAlwaysInViewport ? Host.Viewport.Width + (_showBothScrollIndicator ? -2 : -1) : 0;
  732. }
  733. private void Host_EnabledChanged (object sender, EventArgs e)
  734. {
  735. Enabled = Host.Enabled;
  736. if (_otherScrollBarView is { })
  737. {
  738. _otherScrollBarView.Enabled = Enabled;
  739. }
  740. _contentBottomRightCorner.Enabled = Enabled;
  741. }
  742. private void Host_VisibleChanged (object sender, EventArgs e)
  743. {
  744. if (!Host.Visible)
  745. {
  746. Visible = Host.Visible;
  747. if (_otherScrollBarView is { })
  748. {
  749. _otherScrollBarView.Visible = Visible;
  750. }
  751. _contentBottomRightCorner.Visible = Visible;
  752. }
  753. else
  754. {
  755. ShowHideScrollBars ();
  756. }
  757. }
  758. private void ScrollBarView_Initialized (object sender, EventArgs e)
  759. {
  760. SetWidthHeight ();
  761. SetRelativeLayout (SuperView?.Frame.Size ?? Host?.Frame.Size ?? Frame.Size);
  762. if (OtherScrollBarView is null)
  763. {
  764. // Only do this once if both scrollbars are enabled
  765. ShowHideScrollBars ();
  766. }
  767. SetPosition (Position);
  768. }
  769. // Helper to assist Initialized event handler
  770. private void SetPosition (int newPosition)
  771. {
  772. if (!IsInitialized)
  773. {
  774. // We're not initialized so we can't do anything fancy. Just cache value.
  775. _position = newPosition;
  776. return;
  777. }
  778. if (newPosition < 0)
  779. {
  780. _position = 0;
  781. SetNeedsDisplay ();
  782. return;
  783. }
  784. else if (CanScroll (newPosition - _position, out int max, _vertical))
  785. {
  786. if (max == newPosition - _position)
  787. {
  788. _position = newPosition;
  789. }
  790. else
  791. {
  792. _position = Math.Max (_position + max, 0);
  793. }
  794. }
  795. else if (max < 0)
  796. {
  797. _position = Math.Max (_position + max, 0);
  798. }
  799. else
  800. {
  801. _position = Math.Max (newPosition, 0);
  802. }
  803. OnChangedPosition ();
  804. SetNeedsDisplay ();
  805. }
  806. // BUGBUG: v2 - rationalize this with View.SetMinWidthHeight
  807. private void SetWidthHeight ()
  808. {
  809. // BUGBUG: v2 - If Host is also the ScrollBarView's superview, this is all bogus because it's not
  810. // supported that a view can reference it's superview's Dims. This code also assumes the host does
  811. // not have a margin/borderframe/padding.
  812. if (!IsInitialized || _otherScrollBarView is { IsInitialized: false })
  813. {
  814. return;
  815. }
  816. if (_showBothScrollIndicator)
  817. {
  818. Width = _vertical ? 1 :
  819. Host != SuperView ? Dim.Width (Host) - 1 : Dim.Fill () - 1;
  820. Height = _vertical ? Host != SuperView ? Dim.Height (Host) - 1 : Dim.Fill () - 1 : 1;
  821. _otherScrollBarView.Width = _otherScrollBarView._vertical ? 1 :
  822. Host != SuperView ? Dim.Width (Host) - 1 : Dim.Fill () - 1;
  823. _otherScrollBarView.Height = _otherScrollBarView._vertical
  824. ? Host != SuperView ? Dim.Height (Host) - 1 : Dim.Fill () - 1
  825. : 1;
  826. }
  827. else if (ShowScrollIndicator)
  828. {
  829. Width = _vertical ? 1 :
  830. Host != SuperView ? Dim.Width (Host) : Dim.Fill ();
  831. Height = _vertical ? Host != SuperView ? Dim.Height (Host) : Dim.Fill () : 1;
  832. }
  833. else if (_otherScrollBarView?.ShowScrollIndicator == true)
  834. {
  835. _otherScrollBarView.Width = _otherScrollBarView._vertical ? 1 :
  836. Host != SuperView ? Dim.Width (Host) : Dim.Fill () - 0;
  837. _otherScrollBarView.Height = _otherScrollBarView._vertical
  838. ? Host != SuperView ? Dim.Height (Host) : Dim.Fill () - 0
  839. : 1;
  840. }
  841. }
  842. private void ShowHideScrollBars (bool redraw = true)
  843. {
  844. if (!_hosted || (_hosted && !_autoHideScrollBars))
  845. {
  846. if (_contentBottomRightCorner is { } && _contentBottomRightCorner.Visible)
  847. {
  848. _contentBottomRightCorner.Visible = false;
  849. }
  850. else if (_otherScrollBarView != null
  851. && _otherScrollBarView._contentBottomRightCorner != null
  852. && _otherScrollBarView._contentBottomRightCorner.Visible)
  853. {
  854. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  855. }
  856. return;
  857. }
  858. bool pending = CheckBothScrollBars (this);
  859. if (_otherScrollBarView is { })
  860. {
  861. CheckBothScrollBars (_otherScrollBarView, pending);
  862. }
  863. SetWidthHeight ();
  864. SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  865. if (_otherScrollBarView is { })
  866. {
  867. OtherScrollBarView.SetRelativeLayout (SuperView?.Frame.Size ?? Host.Frame.Size);
  868. }
  869. if (_showBothScrollIndicator)
  870. {
  871. if (_contentBottomRightCorner is { })
  872. {
  873. _contentBottomRightCorner.Visible = true;
  874. }
  875. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  876. {
  877. _otherScrollBarView._contentBottomRightCorner.Visible = true;
  878. }
  879. }
  880. else if (!ShowScrollIndicator)
  881. {
  882. if (_contentBottomRightCorner is { })
  883. {
  884. _contentBottomRightCorner.Visible = false;
  885. }
  886. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  887. {
  888. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  889. }
  890. if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
  891. {
  892. Application.UngrabMouse ();
  893. }
  894. }
  895. else if (_contentBottomRightCorner is { })
  896. {
  897. _contentBottomRightCorner.Visible = false;
  898. }
  899. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { })
  900. {
  901. _otherScrollBarView._contentBottomRightCorner.Visible = false;
  902. }
  903. if (Host?.Visible == true && ShowScrollIndicator && !Visible)
  904. {
  905. Visible = true;
  906. }
  907. if (Host?.Visible == true && _otherScrollBarView?.ShowScrollIndicator == true && !_otherScrollBarView.Visible)
  908. {
  909. _otherScrollBarView.Visible = true;
  910. }
  911. if (!redraw)
  912. {
  913. return;
  914. }
  915. if (ShowScrollIndicator)
  916. {
  917. Draw ();
  918. }
  919. if (_otherScrollBarView is { } && _otherScrollBarView.ShowScrollIndicator)
  920. {
  921. _otherScrollBarView.Draw ();
  922. }
  923. if (_contentBottomRightCorner is { } && _contentBottomRightCorner.Visible)
  924. {
  925. _contentBottomRightCorner.Draw ();
  926. }
  927. else if (_otherScrollBarView is { } && _otherScrollBarView._contentBottomRightCorner is { } && _otherScrollBarView._contentBottomRightCorner.Visible)
  928. {
  929. _otherScrollBarView._contentBottomRightCorner.Draw ();
  930. }
  931. }
  932. internal class ContentBottomRightCorner : View
  933. {
  934. public ContentBottomRightCorner ()
  935. {
  936. ColorScheme = ColorScheme;
  937. }
  938. }
  939. }