ScrollView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. TabStop = TabBehavior.TabGroup;
  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 (GetContentSize ().Height));
  83. AddCommand (Command.BottomEnd, () => ScrollDown (GetContentSize ().Height));
  84. AddCommand (Command.LeftHome, () => ScrollLeft (GetContentSize ().Width));
  85. AddCommand (Command.RightEnd, () => ScrollRight (GetContentSize ().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, GetContentSize ());
  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 (GetContentSize () != value)
  193. // {
  194. // ContentSize = value;
  195. // _contentView.Frame = new Rectangle (_contentOffset, value);
  196. // _vertical.Size = GetContentSize ().Height;
  197. // _horizontal.Size = GetContentSize ().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 > GetContentSize ().Width)
  215. {
  216. p = new Point (
  217. GetContentSize ().Width - Viewport.Width + (_showVerticalScrollIndicator ? 1 : 0),
  218. -_contentOffset.Y
  219. );
  220. }
  221. if (value && -_contentOffset.Y + Viewport.Height > GetContentSize ().Height)
  222. {
  223. if (p == default (Point))
  224. {
  225. p = new Point (
  226. -_contentOffset.X,
  227. GetContentSize ().Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0)
  228. );
  229. }
  230. else
  231. {
  232. p.Y = GetContentSize ().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 View 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. return view;
  326. }
  327. /// <inheritdoc/>
  328. public override void OnDrawContent (Rectangle viewport)
  329. {
  330. SetViewsNeedsDisplay ();
  331. // TODO: It's bad practice for views to always clear a view. It negates clipping.
  332. Clear ();
  333. if (!string.IsNullOrEmpty (_contentView.Text) || _contentView.Subviews.Count > 0)
  334. {
  335. _contentView.Draw ();
  336. }
  337. DrawScrollBars ();
  338. }
  339. /// <inheritdoc/>
  340. public override bool OnKeyDown (Key a)
  341. {
  342. if (base.OnKeyDown (a))
  343. {
  344. return true;
  345. }
  346. bool? result = InvokeKeyBindings (a, KeyBindingScope.HotKey | KeyBindingScope.Focused);
  347. if (result is { })
  348. {
  349. return (bool)result;
  350. }
  351. return false;
  352. }
  353. /// <inheritdoc/>
  354. protected internal override bool OnMouseEvent (MouseEvent me)
  355. {
  356. if (!Enabled)
  357. {
  358. // A disabled view should not eat mouse events
  359. return false;
  360. }
  361. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator)
  362. {
  363. ScrollDown (1);
  364. }
  365. else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator)
  366. {
  367. ScrollUp (1);
  368. }
  369. else if (me.Flags == MouseFlags.WheeledRight && _showHorizontalScrollIndicator)
  370. {
  371. ScrollRight (1);
  372. }
  373. else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator)
  374. {
  375. ScrollLeft (1);
  376. }
  377. else if (me.Position.X == _vertical.Frame.X && ShowVerticalScrollIndicator)
  378. {
  379. _vertical.NewMouseEvent (me);
  380. }
  381. else if (me.Position.Y == _horizontal.Frame.Y && ShowHorizontalScrollIndicator)
  382. {
  383. _horizontal.NewMouseEvent (me);
  384. }
  385. else if (IsOverridden (me.View, "OnMouseEvent"))
  386. {
  387. Application.UngrabMouse ();
  388. }
  389. return base.OnMouseEvent (me);
  390. }
  391. /// <inheritdoc/>
  392. public override Point? PositionCursor ()
  393. {
  394. if (InternalSubviews.Count == 0)
  395. {
  396. Move (0, 0);
  397. return null; // Don't show the cursor
  398. }
  399. return base.PositionCursor ();
  400. }
  401. /// <summary>Removes the view from the scrollview.</summary>
  402. /// <param name="view">The view to remove from the scrollview.</param>
  403. public override View Remove (View view)
  404. {
  405. if (view is null)
  406. {
  407. return view;
  408. }
  409. SetNeedsDisplay ();
  410. View container = view?.SuperView;
  411. if (container == this)
  412. {
  413. base.Remove (view);
  414. }
  415. else
  416. {
  417. container?.Remove (view);
  418. }
  419. if (_contentView.InternalSubviews.Count < 1)
  420. {
  421. CanFocus = false;
  422. }
  423. return view;
  424. }
  425. /// <summary>Removes all widgets from this container.</summary>
  426. public override void RemoveAll () { _contentView.RemoveAll (); }
  427. /// <summary>Scrolls the view down.</summary>
  428. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  429. /// <param name="lines">Number of lines to scroll.</param>
  430. public bool ScrollDown (int lines)
  431. {
  432. if (_vertical.CanScroll (lines, out _, true))
  433. {
  434. ContentOffset = new Point (_contentOffset.X, _contentOffset.Y - lines);
  435. return true;
  436. }
  437. return false;
  438. }
  439. /// <summary>Scrolls the view to the left</summary>
  440. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  441. /// <param name="cols">Number of columns to scroll by.</param>
  442. public bool ScrollLeft (int cols)
  443. {
  444. if (_contentOffset.X < 0)
  445. {
  446. ContentOffset = new Point (Math.Min (_contentOffset.X + cols, 0), _contentOffset.Y);
  447. return true;
  448. }
  449. return false;
  450. }
  451. /// <summary>Scrolls the view to the right.</summary>
  452. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  453. /// <param name="cols">Number of columns to scroll by.</param>
  454. public bool ScrollRight (int cols)
  455. {
  456. if (_horizontal.CanScroll (cols, out _))
  457. {
  458. ContentOffset = new Point (_contentOffset.X - cols, _contentOffset.Y);
  459. return true;
  460. }
  461. return false;
  462. }
  463. /// <summary>Scrolls the view up.</summary>
  464. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  465. /// <param name="lines">Number of lines to scroll.</param>
  466. public bool ScrollUp (int lines)
  467. {
  468. if (_contentOffset.Y < 0)
  469. {
  470. ContentOffset = new Point (_contentOffset.X, Math.Min (_contentOffset.Y + lines, 0));
  471. return true;
  472. }
  473. return false;
  474. }
  475. /// <inheritdoc/>
  476. protected override void Dispose (bool disposing)
  477. {
  478. if (!_showVerticalScrollIndicator)
  479. {
  480. // It was not added to SuperView, so it won't get disposed automatically
  481. _vertical?.Dispose ();
  482. }
  483. if (!_showHorizontalScrollIndicator)
  484. {
  485. // It was not added to SuperView, so it won't get disposed automatically
  486. _horizontal?.Dispose ();
  487. }
  488. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  489. base.Dispose (disposing);
  490. }
  491. private void DrawScrollBars ()
  492. {
  493. if (_autoHideScrollBars)
  494. {
  495. ShowHideScrollBars ();
  496. }
  497. else
  498. {
  499. if (ShowVerticalScrollIndicator)
  500. {
  501. _vertical.Draw ();
  502. }
  503. if (ShowHorizontalScrollIndicator)
  504. {
  505. _horizontal.Draw ();
  506. }
  507. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator)
  508. {
  509. SetContentBottomRightCornerVisibility ();
  510. _contentBottomRightCorner.Draw ();
  511. }
  512. }
  513. }
  514. private void SetContentBottomRightCornerVisibility ()
  515. {
  516. if (_showHorizontalScrollIndicator && _showVerticalScrollIndicator)
  517. {
  518. _contentBottomRightCorner.Visible = true;
  519. }
  520. else if (_horizontal.IsAdded || _vertical.IsAdded)
  521. {
  522. _contentBottomRightCorner.Visible = false;
  523. }
  524. }
  525. private void SetContentOffset (Point offset)
  526. {
  527. // INTENT: Unclear intent. How about a call to Offset?
  528. _contentOffset = new Point (-Math.Abs (offset.X), -Math.Abs (offset.Y));
  529. _contentView.Frame = new Rectangle (_contentOffset, GetContentSize ());
  530. int p = Math.Max (0, -_contentOffset.Y);
  531. if (_vertical.Position != p)
  532. {
  533. _vertical.Position = Math.Max (0, -_contentOffset.Y);
  534. }
  535. p = Math.Max (0, -_contentOffset.X);
  536. if (_horizontal.Position != p)
  537. {
  538. _horizontal.Position = Math.Max (0, -_contentOffset.X);
  539. }
  540. SetNeedsDisplay ();
  541. }
  542. private void SetViewsNeedsDisplay ()
  543. {
  544. foreach (View view in _contentView.Subviews)
  545. {
  546. view.SetNeedsDisplay ();
  547. }
  548. }
  549. private void ShowHideScrollBars ()
  550. {
  551. bool v = false, h = false;
  552. var p = false;
  553. if (GetContentSize () is { } && (Viewport.Height == 0 || Viewport.Height > GetContentSize ().Height))
  554. {
  555. if (ShowVerticalScrollIndicator)
  556. {
  557. ShowVerticalScrollIndicator = false;
  558. }
  559. v = false;
  560. }
  561. else if (GetContentSize () is { } && Viewport.Height > 0 && Viewport.Height == GetContentSize ().Height)
  562. {
  563. p = true;
  564. }
  565. else
  566. {
  567. if (!ShowVerticalScrollIndicator)
  568. {
  569. ShowVerticalScrollIndicator = true;
  570. }
  571. v = true;
  572. }
  573. if (GetContentSize () is { } && (Viewport.Width == 0 || Viewport.Width > GetContentSize ().Width))
  574. {
  575. if (ShowHorizontalScrollIndicator)
  576. {
  577. ShowHorizontalScrollIndicator = false;
  578. }
  579. h = false;
  580. }
  581. else if (GetContentSize () is { } && Viewport.Width > 0 && Viewport.Width == GetContentSize ().Width && p)
  582. {
  583. if (ShowHorizontalScrollIndicator)
  584. {
  585. ShowHorizontalScrollIndicator = false;
  586. }
  587. h = false;
  588. if (ShowVerticalScrollIndicator)
  589. {
  590. ShowVerticalScrollIndicator = false;
  591. }
  592. v = false;
  593. }
  594. else
  595. {
  596. if (p)
  597. {
  598. if (!ShowVerticalScrollIndicator)
  599. {
  600. ShowVerticalScrollIndicator = true;
  601. }
  602. v = true;
  603. }
  604. if (!ShowHorizontalScrollIndicator)
  605. {
  606. ShowHorizontalScrollIndicator = true;
  607. }
  608. h = true;
  609. }
  610. Dim dim = Dim.Fill (h ? 1 : 0);
  611. if (!_vertical.Height.Equals (dim))
  612. {
  613. _vertical.Height = dim;
  614. }
  615. dim = Dim.Fill (v ? 1 : 0);
  616. if (!_horizontal.Width.Equals (dim))
  617. {
  618. _horizontal.Width = dim;
  619. }
  620. if (v)
  621. {
  622. _vertical.SetRelativeLayout (Viewport.Size);
  623. _vertical.Draw ();
  624. }
  625. if (h)
  626. {
  627. _horizontal.SetRelativeLayout (Viewport.Size);
  628. _horizontal.Draw ();
  629. }
  630. SetContentBottomRightCornerVisibility ();
  631. if (v && h)
  632. {
  633. _contentBottomRightCorner.SetRelativeLayout (Viewport.Size);
  634. _contentBottomRightCorner.Draw ();
  635. }
  636. }
  637. private void View_MouseEnter (object sender, MouseEventEventArgs e) { Application.GrabMouse (this); }
  638. private void View_MouseLeave (object sender, MouseEventEventArgs e)
  639. {
  640. if (Application.MouseGrabView is { } && Application.MouseGrabView != this && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal)
  641. {
  642. Application.UngrabMouse ();
  643. }
  644. }
  645. // The ContentView is the view that contains the subviews and content that are being scrolled
  646. // The ContentView is the size of the ContentSize and is offset by the ContentOffset
  647. private class ContentView : View
  648. {
  649. public ContentView () { CanFocus = true; }
  650. }
  651. }