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