ScrollView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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="View.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. Clear ();
  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.NewMouseEvent (me);
  384. }
  385. else if (me.Y == _horizontal.Frame.Y && ShowHorizontalScrollIndicator)
  386. {
  387. _horizontal.NewMouseEvent (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 Point? PositionCursor ()
  397. {
  398. if (InternalSubviews.Count == 0)
  399. {
  400. Move (0, 0);
  401. return Point.Empty;
  402. }
  403. return base.PositionCursor ();
  404. }
  405. /// <summary>Removes the view from the scrollview.</summary>
  406. /// <param name="view">The view to remove from the scrollview.</param>
  407. public override void Remove (View view)
  408. {
  409. if (view is null)
  410. {
  411. return;
  412. }
  413. SetNeedsDisplay ();
  414. View container = view?.SuperView;
  415. if (container == this)
  416. {
  417. base.Remove (view);
  418. }
  419. else
  420. {
  421. container?.Remove (view);
  422. }
  423. if (_contentView.InternalSubviews.Count < 1)
  424. {
  425. CanFocus = false;
  426. }
  427. }
  428. /// <summary>Removes all widgets from this container.</summary>
  429. public override void RemoveAll () { _contentView.RemoveAll (); }
  430. /// <summary>Scrolls the view down.</summary>
  431. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  432. /// <param name="lines">Number of lines to scroll.</param>
  433. public bool ScrollDown (int lines)
  434. {
  435. if (_vertical.CanScroll (lines, out _, true))
  436. {
  437. ContentOffset = new Point (_contentOffset.X, _contentOffset.Y - lines);
  438. return true;
  439. }
  440. return false;
  441. }
  442. /// <summary>Scrolls the view to the left</summary>
  443. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  444. /// <param name="cols">Number of columns to scroll by.</param>
  445. public bool ScrollLeft (int cols)
  446. {
  447. if (_contentOffset.X < 0)
  448. {
  449. ContentOffset = new Point (Math.Min (_contentOffset.X + cols, 0), _contentOffset.Y);
  450. return true;
  451. }
  452. return false;
  453. }
  454. /// <summary>Scrolls the view to the right.</summary>
  455. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  456. /// <param name="cols">Number of columns to scroll by.</param>
  457. public bool ScrollRight (int cols)
  458. {
  459. if (_horizontal.CanScroll (cols, out _))
  460. {
  461. ContentOffset = new Point (_contentOffset.X - cols, _contentOffset.Y);
  462. return true;
  463. }
  464. return false;
  465. }
  466. /// <summary>Scrolls the view up.</summary>
  467. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  468. /// <param name="lines">Number of lines to scroll.</param>
  469. public bool ScrollUp (int lines)
  470. {
  471. if (_contentOffset.Y < 0)
  472. {
  473. ContentOffset = new Point (_contentOffset.X, Math.Min (_contentOffset.Y + lines, 0));
  474. return true;
  475. }
  476. return false;
  477. }
  478. /// <inheritdoc/>
  479. protected override void Dispose (bool disposing)
  480. {
  481. if (!_showVerticalScrollIndicator)
  482. {
  483. // It was not added to SuperView, so it won't get disposed automatically
  484. _vertical?.Dispose ();
  485. }
  486. if (!_showHorizontalScrollIndicator)
  487. {
  488. // It was not added to SuperView, so it won't get disposed automatically
  489. _horizontal?.Dispose ();
  490. }
  491. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  492. base.Dispose (disposing);
  493. }
  494. private void DrawScrollBars ()
  495. {
  496. if (_autoHideScrollBars)
  497. {
  498. ShowHideScrollBars ();
  499. }
  500. else
  501. {
  502. if (ShowVerticalScrollIndicator)
  503. {
  504. _vertical.Draw ();
  505. }
  506. if (ShowHorizontalScrollIndicator)
  507. {
  508. _horizontal.Draw ();
  509. }
  510. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator)
  511. {
  512. SetContentBottomRightCornerVisibility ();
  513. _contentBottomRightCorner.Draw ();
  514. }
  515. }
  516. }
  517. private void SetContentBottomRightCornerVisibility ()
  518. {
  519. if (_showHorizontalScrollIndicator && _showVerticalScrollIndicator)
  520. {
  521. _contentBottomRightCorner.Visible = true;
  522. }
  523. else if (_horizontal.IsAdded || _vertical.IsAdded)
  524. {
  525. _contentBottomRightCorner.Visible = false;
  526. }
  527. }
  528. private void SetContentOffset (Point offset)
  529. {
  530. // INTENT: Unclear intent. How about a call to Offset?
  531. _contentOffset = new Point (-Math.Abs (offset.X), -Math.Abs (offset.Y));
  532. _contentView.Frame = new Rectangle (_contentOffset, ContentSize);
  533. int p = Math.Max (0, -_contentOffset.Y);
  534. if (_vertical.Position != p)
  535. {
  536. _vertical.Position = Math.Max (0, -_contentOffset.Y);
  537. }
  538. p = Math.Max (0, -_contentOffset.X);
  539. if (_horizontal.Position != p)
  540. {
  541. _horizontal.Position = Math.Max (0, -_contentOffset.X);
  542. }
  543. SetNeedsDisplay ();
  544. }
  545. private void SetViewsNeedsDisplay ()
  546. {
  547. foreach (View view in _contentView.Subviews)
  548. {
  549. view.SetNeedsDisplay ();
  550. }
  551. }
  552. private void ShowHideScrollBars ()
  553. {
  554. bool v = false, h = false;
  555. var p = false;
  556. if (Viewport.Height == 0 || Viewport.Height > ContentSize.Height)
  557. {
  558. if (ShowVerticalScrollIndicator)
  559. {
  560. ShowVerticalScrollIndicator = false;
  561. }
  562. v = false;
  563. }
  564. else if (Viewport.Height > 0 && Viewport.Height == ContentSize.Height)
  565. {
  566. p = true;
  567. }
  568. else
  569. {
  570. if (!ShowVerticalScrollIndicator)
  571. {
  572. ShowVerticalScrollIndicator = true;
  573. }
  574. v = true;
  575. }
  576. if (Viewport.Width == 0 || Viewport.Width > ContentSize.Width)
  577. {
  578. if (ShowHorizontalScrollIndicator)
  579. {
  580. ShowHorizontalScrollIndicator = false;
  581. }
  582. h = false;
  583. }
  584. else if (Viewport.Width > 0 && Viewport.Width == ContentSize.Width && p)
  585. {
  586. if (ShowHorizontalScrollIndicator)
  587. {
  588. ShowHorizontalScrollIndicator = false;
  589. }
  590. h = false;
  591. if (ShowVerticalScrollIndicator)
  592. {
  593. ShowVerticalScrollIndicator = false;
  594. }
  595. v = false;
  596. }
  597. else
  598. {
  599. if (p)
  600. {
  601. if (!ShowVerticalScrollIndicator)
  602. {
  603. ShowVerticalScrollIndicator = true;
  604. }
  605. v = true;
  606. }
  607. if (!ShowHorizontalScrollIndicator)
  608. {
  609. ShowHorizontalScrollIndicator = true;
  610. }
  611. h = true;
  612. }
  613. Dim dim = Dim.Fill (h ? 1 : 0);
  614. if (!_vertical.Height.Equals (dim))
  615. {
  616. _vertical.Height = dim;
  617. }
  618. dim = Dim.Fill (v ? 1 : 0);
  619. if (!_horizontal.Width.Equals (dim))
  620. {
  621. _horizontal.Width = dim;
  622. }
  623. if (v)
  624. {
  625. _vertical.SetRelativeLayout (Viewport.Size);
  626. _vertical.Draw ();
  627. }
  628. if (h)
  629. {
  630. _horizontal.SetRelativeLayout (Viewport.Size);
  631. _horizontal.Draw ();
  632. }
  633. SetContentBottomRightCornerVisibility ();
  634. if (v && h)
  635. {
  636. _contentBottomRightCorner.SetRelativeLayout (Viewport.Size);
  637. _contentBottomRightCorner.Draw ();
  638. }
  639. }
  640. private void View_MouseEnter (object sender, MouseEventEventArgs e) { Application.GrabMouse (this); }
  641. private void View_MouseLeave (object sender, MouseEventEventArgs e)
  642. {
  643. if (Application.MouseGrabView is { } && Application.MouseGrabView != this && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal)
  644. {
  645. Application.UngrabMouse ();
  646. }
  647. }
  648. // The ContentView is the view that contains the subviews and content that are being scrolled
  649. // The ContentView is the size of the ContentSize and is offset by the ContentOffset
  650. private class ContentView : View
  651. {
  652. public ContentView () { CanFocus = true; }
  653. }
  654. }