ScrollView.cs 23 KB

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