ScrollView.cs 23 KB

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