ScrollBarView.cs 35 KB

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