ScrollBarView.cs 35 KB

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