ScrollView.cs 24 KB

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