ScrollView.cs 24 KB

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