ScrollView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //
  2. // ScrollView.cs: ScrollView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // - focus in scrollview
  10. // - focus handling in scrollview to auto scroll to focused view
  11. // - Raise events
  12. // - Perhaps allow an option to not display the scrollbar arrow indicators?
  13. namespace Terminal.Gui;
  14. /// <summary>
  15. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS
  16. /// UIScrollView.
  17. /// </summary>
  18. /// <remarks>
  19. /// <para>
  20. /// The subviews that are added to this <see cref="Gui.ScrollView"/> are offset by the
  21. /// <see cref="ContentOffset"/> property. The view itself is a window into the space represented by the
  22. /// <see cref="ContentSize"/>.
  23. /// </para>
  24. /// <para>Use the</para>
  25. /// </remarks>
  26. public class ScrollView : View
  27. {
  28. private readonly ContentView _contentView;
  29. private readonly ScrollBarView _horizontal;
  30. private readonly ScrollBarView _vertical;
  31. private bool _autoHideScrollBars = true;
  32. private View _contentBottomRightCorner;
  33. private Point _contentOffset;
  34. private bool _keepContentAlwaysInViewport = true;
  35. private bool _showHorizontalScrollIndicator;
  36. private bool _showVerticalScrollIndicator;
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/>
  39. /// positioning.
  40. /// </summary>
  41. public ScrollView ()
  42. {
  43. _contentView = new ContentView ();
  44. _vertical = new ScrollBarView
  45. {
  46. X = Pos.AnchorEnd (1),
  47. Y = 0,
  48. Width = 1,
  49. Height = Dim.Fill (_showHorizontalScrollIndicator ? 1 : 0),
  50. Size = 1,
  51. IsVertical = true,
  52. Host = this
  53. };
  54. _horizontal = new ScrollBarView
  55. {
  56. X = 0,
  57. Y = Pos.AnchorEnd (1),
  58. Width = Dim.Fill (_showVerticalScrollIndicator ? 1 : 0),
  59. Height = 1,
  60. Size = 1,
  61. IsVertical = false,
  62. Host = this
  63. };
  64. _vertical.OtherScrollBarView = _horizontal;
  65. _horizontal.OtherScrollBarView = _vertical;
  66. base.Add (_contentView);
  67. CanFocus = true;
  68. MouseEnter += View_MouseEnter;
  69. MouseLeave += View_MouseLeave;
  70. _contentView.MouseEnter += View_MouseEnter;
  71. _contentView.MouseLeave += View_MouseLeave;
  72. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  73. // Things this view knows how to do
  74. AddCommand (Command.ScrollUp, () => ScrollUp (1));
  75. AddCommand (Command.ScrollDown, () => ScrollDown (1));
  76. AddCommand (Command.ScrollLeft, () => ScrollLeft (1));
  77. AddCommand (Command.ScrollRight, () => ScrollRight (1));
  78. AddCommand (Command.PageUp, () => ScrollUp (Viewport.Height));
  79. AddCommand (Command.PageDown, () => ScrollDown (Viewport.Height));
  80. AddCommand (Command.PageLeft, () => ScrollLeft (Viewport.Width));
  81. AddCommand (Command.PageRight, () => ScrollRight (Viewport.Width));
  82. AddCommand (Command.TopHome, () => ScrollUp (ContentSize.Height));
  83. AddCommand (Command.BottomEnd, () => ScrollDown (ContentSize.Height));
  84. AddCommand (Command.LeftHome, () => ScrollLeft (ContentSize.Width));
  85. AddCommand (Command.RightEnd, () => ScrollRight (ContentSize.Width));
  86. // Default keybindings for this view
  87. KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
  88. KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
  89. KeyBindings.Add (Key.CursorLeft, Command.ScrollLeft);
  90. KeyBindings.Add (Key.CursorRight, Command.ScrollRight);
  91. KeyBindings.Add (Key.PageUp, Command.PageUp);
  92. KeyBindings.Add (Key.V.WithAlt, Command.PageUp);
  93. KeyBindings.Add (Key.PageDown, Command.PageDown);
  94. KeyBindings.Add (Key.V.WithCtrl, Command.PageDown);
  95. KeyBindings.Add (Key.PageUp.WithCtrl, Command.PageLeft);
  96. KeyBindings.Add (Key.PageDown.WithCtrl, Command.PageRight);
  97. KeyBindings.Add (Key.Home, Command.TopHome);
  98. KeyBindings.Add (Key.End, Command.BottomEnd);
  99. KeyBindings.Add (Key.Home.WithCtrl, Command.LeftHome);
  100. KeyBindings.Add (Key.End.WithCtrl, Command.RightEnd);
  101. Initialized += (s, e) =>
  102. {
  103. if (!_vertical.IsInitialized)
  104. {
  105. _vertical.BeginInit ();
  106. _vertical.EndInit ();
  107. }
  108. if (!_horizontal.IsInitialized)
  109. {
  110. _horizontal.BeginInit ();
  111. _horizontal.EndInit ();
  112. }
  113. SetContentOffset (_contentOffset);
  114. _contentView.Frame = new Rectangle (ContentOffset, ContentSize);
  115. // PERF: How about calls to Point.Offset instead?
  116. _vertical.ChangedPosition += delegate { ContentOffset = new Point (ContentOffset.X, _vertical.Position); };
  117. _horizontal.ChangedPosition += delegate { ContentOffset = new Point (_horizontal.Position, ContentOffset.Y); };
  118. };
  119. ContentSizeChanged += ScrollViewContentSizeChanged;
  120. }
  121. private void ScrollViewContentSizeChanged (object sender, SizeChangedEventArgs e)
  122. {
  123. _contentView.Frame = new Rectangle (ContentOffset, e.Size with {Width = e.Size.Width-1, Height = e.Size.Height-1});
  124. _vertical.Size = e.Size.Height;
  125. _horizontal.Size = e.Size.Width;
  126. }
  127. private void Application_UnGrabbedMouse (object sender, ViewEventArgs e)
  128. {
  129. var parent = e.View is Adornment adornment ? adornment.Parent : e.View;
  130. if (parent is { })
  131. {
  132. var supView = parent.SuperView;
  133. while (supView is { })
  134. {
  135. if (supView == _contentView)
  136. {
  137. Application.GrabMouse (this);
  138. break;
  139. }
  140. supView = supView.SuperView;
  141. }
  142. }
  143. }
  144. /// <summary>If true the vertical/horizontal scroll bars won't be showed if it's not needed.</summary>
  145. public bool AutoHideScrollBars
  146. {
  147. get => _autoHideScrollBars;
  148. set
  149. {
  150. if (_autoHideScrollBars != value)
  151. {
  152. _autoHideScrollBars = value;
  153. if (Subviews.Contains (_vertical))
  154. {
  155. _vertical.AutoHideScrollBars = value;
  156. }
  157. if (Subviews.Contains (_horizontal))
  158. {
  159. _horizontal.AutoHideScrollBars = value;
  160. }
  161. SetNeedsDisplay ();
  162. }
  163. }
  164. }
  165. /// <summary>Represents the top left corner coordinate that is displayed by the scrollview</summary>
  166. /// <value>The content offset.</value>
  167. public Point ContentOffset
  168. {
  169. get => _contentOffset;
  170. set
  171. {
  172. if (!IsInitialized)
  173. {
  174. // We're not initialized so we can't do anything fancy. Just cache value.
  175. _contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y));
  176. return;
  177. }
  178. SetContentOffset (value);
  179. }
  180. }
  181. ///// <summary>Represents the contents of the data shown inside the scrollview</summary>
  182. ///// <value>The size of the content.</value>
  183. //public new Size ContentSize
  184. //{
  185. // get => ContentSize;
  186. // set
  187. // {
  188. // if (ContentSize != value)
  189. // {
  190. // ContentSize = value;
  191. // _contentView.Frame = new Rectangle (_contentOffset, value);
  192. // _vertical.Size = ContentSize.Height;
  193. // _horizontal.Size = ContentSize.Width;
  194. // SetNeedsDisplay ();
  195. // }
  196. // }
  197. //}
  198. /// <summary>Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollView"/></summary>
  199. public bool KeepContentAlwaysInViewport
  200. {
  201. get => _keepContentAlwaysInViewport;
  202. set
  203. {
  204. if (_keepContentAlwaysInViewport != value)
  205. {
  206. _keepContentAlwaysInViewport = value;
  207. _vertical.OtherScrollBarView.KeepContentAlwaysInViewport = value;
  208. _horizontal.OtherScrollBarView.KeepContentAlwaysInViewport = value;
  209. Point p = default;
  210. if (value && -_contentOffset.X + Viewport.Width > ContentSize.Width)
  211. {
  212. p = new Point (
  213. ContentSize.Width - Viewport.Width + (_showVerticalScrollIndicator ? 1 : 0),
  214. -_contentOffset.Y
  215. );
  216. }
  217. if (value && -_contentOffset.Y + Viewport.Height > ContentSize.Height)
  218. {
  219. if (p == default (Point))
  220. {
  221. p = new Point (
  222. -_contentOffset.X,
  223. ContentSize.Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0)
  224. );
  225. }
  226. else
  227. {
  228. p.Y = ContentSize.Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0);
  229. }
  230. }
  231. if (p != default (Point))
  232. {
  233. ContentOffset = p;
  234. }
  235. }
  236. }
  237. }
  238. /// <summary>Gets or sets the visibility for the horizontal scroll indicator.</summary>
  239. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  240. public bool ShowHorizontalScrollIndicator
  241. {
  242. get => _showHorizontalScrollIndicator;
  243. set
  244. {
  245. if (value != _showHorizontalScrollIndicator)
  246. {
  247. _showHorizontalScrollIndicator = value;
  248. SetNeedsLayout ();
  249. if (value)
  250. {
  251. _horizontal.OtherScrollBarView = _vertical;
  252. base.Add (_horizontal);
  253. _horizontal.ShowScrollIndicator = value;
  254. _horizontal.AutoHideScrollBars = _autoHideScrollBars;
  255. _horizontal.OtherScrollBarView.ShowScrollIndicator = value;
  256. _horizontal.MouseEnter += View_MouseEnter;
  257. _horizontal.MouseLeave += View_MouseLeave;
  258. }
  259. else
  260. {
  261. base.Remove (_horizontal);
  262. _horizontal.OtherScrollBarView = null;
  263. _horizontal.MouseEnter -= View_MouseEnter;
  264. _horizontal.MouseLeave -= View_MouseLeave;
  265. }
  266. }
  267. _vertical.Height = Dim.Fill (_showHorizontalScrollIndicator ? 1 : 0);
  268. }
  269. }
  270. /// <summary>Gets or sets the visibility for the vertical scroll indicator.</summary>
  271. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  272. public bool ShowVerticalScrollIndicator
  273. {
  274. get => _showVerticalScrollIndicator;
  275. set
  276. {
  277. if (value != _showVerticalScrollIndicator)
  278. {
  279. _showVerticalScrollIndicator = value;
  280. SetNeedsLayout ();
  281. if (value)
  282. {
  283. _vertical.OtherScrollBarView = _horizontal;
  284. base.Add (_vertical);
  285. _vertical.ShowScrollIndicator = value;
  286. _vertical.AutoHideScrollBars = _autoHideScrollBars;
  287. _vertical.OtherScrollBarView.ShowScrollIndicator = value;
  288. _vertical.MouseEnter += View_MouseEnter;
  289. _vertical.MouseLeave += View_MouseLeave;
  290. }
  291. else
  292. {
  293. Remove (_vertical);
  294. _vertical.OtherScrollBarView = null;
  295. _vertical.MouseEnter -= View_MouseEnter;
  296. _vertical.MouseLeave -= View_MouseLeave;
  297. }
  298. }
  299. _horizontal.Width = Dim.Fill (_showVerticalScrollIndicator ? 1 : 0);
  300. }
  301. }
  302. /// <summary>Adds the view to the scrollview.</summary>
  303. /// <param name="view">The view to add to the scrollview.</param>
  304. public override void Add (View view)
  305. {
  306. if (view is ScrollBarView.ContentBottomRightCorner)
  307. {
  308. _contentBottomRightCorner = view;
  309. base.Add (view);
  310. }
  311. else
  312. {
  313. if (!IsOverridden (view, "OnMouseEvent"))
  314. {
  315. view.MouseEnter += View_MouseEnter;
  316. view.MouseLeave += View_MouseLeave;
  317. }
  318. _contentView.Add (view);
  319. }
  320. SetNeedsLayout ();
  321. }
  322. /// <inheritdoc/>
  323. public override void OnDrawContent (Rectangle viewport)
  324. {
  325. SetViewsNeedsDisplay ();
  326. // TODO: It's bad practice for views to always clear a view. It negates clipping.
  327. ClearVisibleContent();
  328. if (!string.IsNullOrEmpty (_contentView.Text) || _contentView.Subviews.Count > 0)
  329. {
  330. _contentView.Draw ();
  331. }
  332. DrawScrollBars ();
  333. }
  334. /// <inheritdoc/>
  335. public override bool OnEnter (View view)
  336. {
  337. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus))
  338. {
  339. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  340. }
  341. return base.OnEnter (view);
  342. }
  343. /// <inheritdoc/>
  344. public override bool OnKeyDown (Key a)
  345. {
  346. if (base.OnKeyDown (a))
  347. {
  348. return true;
  349. }
  350. bool? result = InvokeKeyBindings (a);
  351. if (result is { })
  352. {
  353. return (bool)result;
  354. }
  355. return false;
  356. }
  357. /// <inheritdoc/>
  358. protected internal override bool OnMouseEvent (MouseEvent me)
  359. {
  360. if (!Enabled)
  361. {
  362. // A disabled view should not eat mouse events
  363. return false;
  364. }
  365. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator)
  366. {
  367. ScrollDown (1);
  368. }
  369. else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator)
  370. {
  371. ScrollUp (1);
  372. }
  373. else if (me.Flags == MouseFlags.WheeledRight && _showHorizontalScrollIndicator)
  374. {
  375. ScrollRight (1);
  376. }
  377. else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator)
  378. {
  379. ScrollLeft (1);
  380. }
  381. else if (me.X == _vertical.Frame.X && ShowVerticalScrollIndicator)
  382. {
  383. _vertical.OnMouseEvent (me);
  384. }
  385. else if (me.Y == _horizontal.Frame.Y && ShowHorizontalScrollIndicator)
  386. {
  387. _horizontal.OnMouseEvent (me);
  388. }
  389. else if (IsOverridden (me.View, "OnMouseEvent"))
  390. {
  391. Application.UngrabMouse ();
  392. }
  393. return base.OnMouseEvent(me);
  394. }
  395. /// <inheritdoc/>
  396. public override void PositionCursor ()
  397. {
  398. if (InternalSubviews.Count == 0)
  399. {
  400. Move (0, 0);
  401. }
  402. else
  403. {
  404. base.PositionCursor ();
  405. }
  406. }
  407. /// <summary>Removes the view from the scrollview.</summary>
  408. /// <param name="view">The view to remove from the scrollview.</param>
  409. public override void Remove (View view)
  410. {
  411. if (view is null)
  412. {
  413. return;
  414. }
  415. SetNeedsDisplay ();
  416. View container = view?.SuperView;
  417. if (container == this)
  418. {
  419. base.Remove (view);
  420. }
  421. else
  422. {
  423. container?.Remove (view);
  424. }
  425. if (_contentView.InternalSubviews.Count < 1)
  426. {
  427. CanFocus = false;
  428. }
  429. }
  430. /// <summary>Removes all widgets from this container.</summary>
  431. public override void RemoveAll () { _contentView.RemoveAll (); }
  432. /// <summary>Scrolls the view down.</summary>
  433. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  434. /// <param name="lines">Number of lines to scroll.</param>
  435. public bool ScrollDown (int lines)
  436. {
  437. if (_vertical.CanScroll (lines, out _, true))
  438. {
  439. ContentOffset = new Point (_contentOffset.X, _contentOffset.Y - lines);
  440. return true;
  441. }
  442. return false;
  443. }
  444. /// <summary>Scrolls the view to the left</summary>
  445. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  446. /// <param name="cols">Number of columns to scroll by.</param>
  447. public bool ScrollLeft (int cols)
  448. {
  449. if (_contentOffset.X < 0)
  450. {
  451. ContentOffset = new Point (Math.Min (_contentOffset.X + cols, 0), _contentOffset.Y);
  452. return true;
  453. }
  454. return false;
  455. }
  456. /// <summary>Scrolls the view to the right.</summary>
  457. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  458. /// <param name="cols">Number of columns to scroll by.</param>
  459. public bool ScrollRight (int cols)
  460. {
  461. if (_horizontal.CanScroll (cols, out _))
  462. {
  463. ContentOffset = new Point (_contentOffset.X - cols, _contentOffset.Y);
  464. return true;
  465. }
  466. return false;
  467. }
  468. /// <summary>Scrolls the view up.</summary>
  469. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  470. /// <param name="lines">Number of lines to scroll.</param>
  471. public bool ScrollUp (int lines)
  472. {
  473. if (_contentOffset.Y < 0)
  474. {
  475. ContentOffset = new Point (_contentOffset.X, Math.Min (_contentOffset.Y + lines, 0));
  476. return true;
  477. }
  478. return false;
  479. }
  480. /// <inheritdoc/>
  481. protected override void Dispose (bool disposing)
  482. {
  483. if (!_showVerticalScrollIndicator)
  484. {
  485. // It was not added to SuperView, so it won't get disposed automatically
  486. _vertical?.Dispose ();
  487. }
  488. if (!_showHorizontalScrollIndicator)
  489. {
  490. // It was not added to SuperView, so it won't get disposed automatically
  491. _horizontal?.Dispose ();
  492. }
  493. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  494. base.Dispose (disposing);
  495. }
  496. private void DrawScrollBars ()
  497. {
  498. if (_autoHideScrollBars)
  499. {
  500. ShowHideScrollBars ();
  501. }
  502. else
  503. {
  504. if (ShowVerticalScrollIndicator)
  505. {
  506. _vertical.Draw ();
  507. }
  508. if (ShowHorizontalScrollIndicator)
  509. {
  510. _horizontal.Draw ();
  511. }
  512. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator)
  513. {
  514. SetContentBottomRightCornerVisibility ();
  515. _contentBottomRightCorner.Draw ();
  516. }
  517. }
  518. }
  519. private void SetContentBottomRightCornerVisibility ()
  520. {
  521. if (_showHorizontalScrollIndicator && _showVerticalScrollIndicator)
  522. {
  523. _contentBottomRightCorner.Visible = true;
  524. }
  525. else if (_horizontal.IsAdded || _vertical.IsAdded)
  526. {
  527. _contentBottomRightCorner.Visible = false;
  528. }
  529. }
  530. private void SetContentOffset (Point offset)
  531. {
  532. // INTENT: Unclear intent. How about a call to Offset?
  533. _contentOffset = new Point (-Math.Abs (offset.X), -Math.Abs (offset.Y));
  534. _contentView.Frame = new Rectangle (_contentOffset, ContentSize);
  535. int p = Math.Max (0, -_contentOffset.Y);
  536. if (_vertical.Position != p)
  537. {
  538. _vertical.Position = Math.Max (0, -_contentOffset.Y);
  539. }
  540. p = Math.Max (0, -_contentOffset.X);
  541. if (_horizontal.Position != p)
  542. {
  543. _horizontal.Position = Math.Max (0, -_contentOffset.X);
  544. }
  545. SetNeedsDisplay ();
  546. }
  547. private void SetViewsNeedsDisplay ()
  548. {
  549. foreach (View view in _contentView.Subviews)
  550. {
  551. view.SetNeedsDisplay ();
  552. }
  553. }
  554. private void ShowHideScrollBars ()
  555. {
  556. bool v = false, h = false;
  557. var p = false;
  558. if (Viewport.Height == 0 || Viewport.Height > ContentSize.Height)
  559. {
  560. if (ShowVerticalScrollIndicator)
  561. {
  562. ShowVerticalScrollIndicator = false;
  563. }
  564. v = false;
  565. }
  566. else if (Viewport.Height > 0 && Viewport.Height == ContentSize.Height)
  567. {
  568. p = true;
  569. }
  570. else
  571. {
  572. if (!ShowVerticalScrollIndicator)
  573. {
  574. ShowVerticalScrollIndicator = true;
  575. }
  576. v = true;
  577. }
  578. if (Viewport.Width == 0 || Viewport.Width > ContentSize.Width)
  579. {
  580. if (ShowHorizontalScrollIndicator)
  581. {
  582. ShowHorizontalScrollIndicator = false;
  583. }
  584. h = false;
  585. }
  586. else if (Viewport.Width > 0 && Viewport.Width == ContentSize.Width && p)
  587. {
  588. if (ShowHorizontalScrollIndicator)
  589. {
  590. ShowHorizontalScrollIndicator = false;
  591. }
  592. h = false;
  593. if (ShowVerticalScrollIndicator)
  594. {
  595. ShowVerticalScrollIndicator = false;
  596. }
  597. v = false;
  598. }
  599. else
  600. {
  601. if (p)
  602. {
  603. if (!ShowVerticalScrollIndicator)
  604. {
  605. ShowVerticalScrollIndicator = true;
  606. }
  607. v = true;
  608. }
  609. if (!ShowHorizontalScrollIndicator)
  610. {
  611. ShowHorizontalScrollIndicator = true;
  612. }
  613. h = true;
  614. }
  615. Dim dim = Dim.Fill (h ? 1 : 0);
  616. if (!_vertical.Height.Equals (dim))
  617. {
  618. _vertical.Height = dim;
  619. }
  620. dim = Dim.Fill (v ? 1 : 0);
  621. if (!_horizontal.Width.Equals (dim))
  622. {
  623. _horizontal.Width = dim;
  624. }
  625. if (v)
  626. {
  627. _vertical.SetRelativeLayout (Viewport.Size);
  628. _vertical.Draw ();
  629. }
  630. if (h)
  631. {
  632. _horizontal.SetRelativeLayout (Viewport.Size);
  633. _horizontal.Draw ();
  634. }
  635. SetContentBottomRightCornerVisibility ();
  636. if (v && h)
  637. {
  638. _contentBottomRightCorner.SetRelativeLayout (Viewport.Size);
  639. _contentBottomRightCorner.Draw ();
  640. }
  641. }
  642. private void View_MouseEnter (object sender, MouseEventEventArgs e) { Application.GrabMouse (this); }
  643. private void View_MouseLeave (object sender, MouseEventEventArgs e)
  644. {
  645. if (Application.MouseGrabView is { } && Application.MouseGrabView != this && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal)
  646. {
  647. Application.UngrabMouse ();
  648. }
  649. }
  650. // The ContentView is the view that contains the subviews and content that are being scrolled
  651. // The ContentView is the size of the ContentSize and is offset by the ContentOffset
  652. private class ContentView : View
  653. {
  654. public ContentView () { CanFocus = true; }
  655. }
  656. }