ScrollView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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;
  14. using System.Linq;
  15. namespace Terminal.Gui {
  16. /// <summary>
  17. /// Scrollviews are views that present a window into a virtual space where subviews are added. Similar to the iOS 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
  23. /// space represented by the <see cref="ContentSize"/>.
  24. /// </para>
  25. /// <para>
  26. /// Use the
  27. /// </para>
  28. /// </remarks>
  29. public class ScrollView : View {
  30. // The ContentView is the view that contains the subviews and content that are being scrolled
  31. // The ContentView is the size of the ContentSize and is offset by the ContentOffset
  32. private class ContentView : View {
  33. public ContentView (Rect frame) : base (frame)
  34. {
  35. Id = "ScrollView.ContentView";
  36. CanFocus = true;
  37. }
  38. }
  39. ContentView contentView;
  40. ScrollBarView vertical, horizontal;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Absolute"/> positioning.
  43. /// </summary>
  44. /// <param name="frame"></param>
  45. public ScrollView (Rect frame) : base (frame)
  46. {
  47. SetInitialProperties (frame);
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="Gui.ScrollView"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  51. /// </summary>
  52. public ScrollView () : base ()
  53. {
  54. SetInitialProperties (Rect.Empty);
  55. }
  56. void SetInitialProperties (Rect frame)
  57. {
  58. contentView = new ContentView (frame);
  59. vertical = new ScrollBarView (1, 0, isVertical: true) {
  60. X = Pos.AnchorEnd (1),
  61. Y = 0,
  62. Width = 1,
  63. Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0)
  64. };
  65. vertical.Host = this;
  66. horizontal = new ScrollBarView (1, 0, isVertical: false) {
  67. X = 0,
  68. Y = Pos.AnchorEnd (1),
  69. Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0),
  70. Height = 1
  71. };
  72. horizontal.Host = this;
  73. vertical.OtherScrollBarView = horizontal;
  74. horizontal.OtherScrollBarView = vertical;
  75. base.Add (contentView);
  76. CanFocus = true;
  77. MouseEnter += View_MouseEnter;
  78. MouseLeave += View_MouseLeave;
  79. contentView.MouseEnter += View_MouseEnter;
  80. contentView.MouseLeave += View_MouseLeave;
  81. // Things this view knows how to do
  82. AddCommand (Command.ScrollUp, () => ScrollUp (1));
  83. AddCommand (Command.ScrollDown, () => ScrollDown (1));
  84. AddCommand (Command.ScrollLeft, () => ScrollLeft (1));
  85. AddCommand (Command.ScrollRight, () => ScrollRight (1));
  86. AddCommand (Command.PageUp, () => ScrollUp (Bounds.Height));
  87. AddCommand (Command.PageDown, () => ScrollDown (Bounds.Height));
  88. AddCommand (Command.PageLeft, () => ScrollLeft (Bounds.Width));
  89. AddCommand (Command.PageRight, () => ScrollRight (Bounds.Width));
  90. AddCommand (Command.TopHome, () => ScrollUp (contentSize.Height));
  91. AddCommand (Command.BottomEnd, () => ScrollDown (contentSize.Height));
  92. AddCommand (Command.LeftHome, () => ScrollLeft (contentSize.Width));
  93. AddCommand (Command.RightEnd, () => ScrollRight (contentSize.Width));
  94. // Default keybindings for this view
  95. AddKeyBinding (Key.CursorUp, Command.ScrollUp);
  96. AddKeyBinding (Key.CursorDown, Command.ScrollDown);
  97. AddKeyBinding (Key.CursorLeft, Command.ScrollLeft);
  98. AddKeyBinding (Key.CursorRight, Command.ScrollRight);
  99. AddKeyBinding (Key.PageUp, Command.PageUp);
  100. AddKeyBinding ((Key)'v' | Key.AltMask, Command.PageUp);
  101. AddKeyBinding (Key.PageDown, Command.PageDown);
  102. AddKeyBinding (Key.V | Key.CtrlMask, Command.PageDown);
  103. AddKeyBinding (Key.PageUp | Key.CtrlMask, Command.PageLeft);
  104. AddKeyBinding (Key.PageDown | Key.CtrlMask, Command.PageRight);
  105. AddKeyBinding (Key.Home, Command.TopHome);
  106. AddKeyBinding (Key.End, Command.BottomEnd);
  107. AddKeyBinding (Key.Home | Key.CtrlMask, Command.LeftHome);
  108. AddKeyBinding (Key.End | Key.CtrlMask, Command.RightEnd);
  109. Initialized += (s, e) => {
  110. if (!vertical.IsInitialized) {
  111. vertical.BeginInit ();
  112. vertical.EndInit ();
  113. }
  114. if (!horizontal.IsInitialized) {
  115. horizontal.BeginInit ();
  116. horizontal.EndInit ();
  117. }
  118. SetContentOffset (contentOffset);
  119. contentView.Frame = new Rect (ContentOffset, ContentSize);
  120. vertical.ChangedPosition += delegate {
  121. ContentOffset = new Point (ContentOffset.X, vertical.Position);
  122. };
  123. horizontal.ChangedPosition += delegate {
  124. ContentOffset = new Point (horizontal.Position, ContentOffset.Y);
  125. };
  126. };
  127. }
  128. //public override void BeginInit ()
  129. //{
  130. // SetContentOffset (contentOffset);
  131. // base.BeginInit ();
  132. //}
  133. Size contentSize;
  134. Point contentOffset;
  135. bool showHorizontalScrollIndicator;
  136. bool showVerticalScrollIndicator;
  137. bool keepContentAlwaysInViewport = true;
  138. bool autoHideScrollBars = true;
  139. /// <summary>
  140. /// Represents the contents of the data shown inside the scrollview
  141. /// </summary>
  142. /// <value>The size of the content.</value>
  143. public Size ContentSize {
  144. get {
  145. return contentSize;
  146. }
  147. set {
  148. if (contentSize != value) {
  149. contentSize = value;
  150. contentView.Frame = new Rect (contentOffset, value);
  151. vertical.Size = contentSize.Height;
  152. horizontal.Size = contentSize.Width;
  153. SetNeedsDisplay ();
  154. }
  155. }
  156. }
  157. /// <summary>
  158. /// Represents the top left corner coordinate that is displayed by the scrollview
  159. /// </summary>
  160. /// <value>The content offset.</value>
  161. public Point ContentOffset {
  162. get {
  163. return contentOffset;
  164. }
  165. set {
  166. if (!IsInitialized) {
  167. // We're not initialized so we can't do anything fancy. Just cache value.
  168. contentOffset = new Point (-Math.Abs (value.X), -Math.Abs (value.Y)); ;
  169. return;
  170. }
  171. SetContentOffset (value);
  172. }
  173. }
  174. private void SetContentOffset (Point offset)
  175. {
  176. var co = new Point (-Math.Abs (offset.X), -Math.Abs (offset.Y));
  177. contentOffset = co;
  178. contentView.Frame = new Rect (contentOffset, contentSize);
  179. var p = Math.Max (0, -contentOffset.Y);
  180. if (vertical.Position != p) {
  181. vertical.Position = Math.Max (0, -contentOffset.Y);
  182. }
  183. p = Math.Max (0, -contentOffset.X);
  184. if (horizontal.Position != p) {
  185. horizontal.Position = Math.Max (0, -contentOffset.X);
  186. }
  187. SetNeedsDisplay ();
  188. }
  189. /// <summary>
  190. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  191. /// </summary>
  192. public bool AutoHideScrollBars {
  193. get => autoHideScrollBars;
  194. set {
  195. if (autoHideScrollBars != value) {
  196. autoHideScrollBars = value;
  197. if (Subviews.Contains (vertical)) {
  198. vertical.AutoHideScrollBars = value;
  199. }
  200. if (Subviews.Contains (horizontal)) {
  201. horizontal.AutoHideScrollBars = value;
  202. }
  203. SetNeedsDisplay ();
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollView"/>
  209. /// </summary>
  210. public bool KeepContentAlwaysInViewport {
  211. get { return keepContentAlwaysInViewport; }
  212. set {
  213. if (keepContentAlwaysInViewport != value) {
  214. keepContentAlwaysInViewport = value;
  215. vertical.OtherScrollBarView.KeepContentAlwaysInViewport = value;
  216. horizontal.OtherScrollBarView.KeepContentAlwaysInViewport = value;
  217. Point p = default;
  218. if (value && -contentOffset.X + Bounds.Width > contentSize.Width) {
  219. p = new Point (contentSize.Width - Bounds.Width + (showVerticalScrollIndicator ? 1 : 0), -contentOffset.Y);
  220. }
  221. if (value && -contentOffset.Y + Bounds.Height > contentSize.Height) {
  222. if (p == default) {
  223. p = new Point (-contentOffset.X, contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0));
  224. } else {
  225. p.Y = contentSize.Height - Bounds.Height + (showHorizontalScrollIndicator ? 1 : 0);
  226. }
  227. }
  228. if (p != default) {
  229. ContentOffset = p;
  230. }
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Adds the view to the scrollview.
  236. /// </summary>
  237. /// <param name="view">The view to add to the scrollview.</param>
  238. public override void Add (View view)
  239. {
  240. if (view.Id == "contentBottomRightCorner") {
  241. base.Add (view);
  242. } else {
  243. if (!IsOverridden (view, "MouseEvent")) {
  244. view.MouseEnter += View_MouseEnter;
  245. view.MouseLeave += View_MouseLeave;
  246. }
  247. contentView.Add (view);
  248. }
  249. SetNeedsLayout ();
  250. }
  251. void View_MouseLeave (object sender, MouseEventEventArgs e)
  252. {
  253. if (Application.MouseGrabView != null && Application.MouseGrabView != vertical && Application.MouseGrabView != horizontal) {
  254. Application.UngrabMouse ();
  255. }
  256. }
  257. void View_MouseEnter (object sender, MouseEventEventArgs e)
  258. {
  259. Application.GrabMouse (this);
  260. }
  261. /// <summary>
  262. /// Gets or sets the visibility for the horizontal scroll indicator.
  263. /// </summary>
  264. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  265. public bool ShowHorizontalScrollIndicator {
  266. get => showHorizontalScrollIndicator;
  267. set {
  268. if (value != showHorizontalScrollIndicator) {
  269. showHorizontalScrollIndicator = value;
  270. SetNeedsLayout ();
  271. if (value) {
  272. horizontal.OtherScrollBarView = vertical;
  273. base.Add (horizontal);
  274. horizontal.ShowScrollIndicator = value;
  275. horizontal.AutoHideScrollBars = autoHideScrollBars;
  276. horizontal.OtherScrollBarView.ShowScrollIndicator = value;
  277. horizontal.MouseEnter += View_MouseEnter;
  278. horizontal.MouseLeave += View_MouseLeave;
  279. } else {
  280. base.Remove (horizontal);
  281. horizontal.OtherScrollBarView = null;
  282. horizontal.MouseEnter -= View_MouseEnter;
  283. horizontal.MouseLeave -= View_MouseLeave;
  284. }
  285. }
  286. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  287. }
  288. }
  289. /// <summary>
  290. /// Removes all widgets from this container.
  291. /// </summary>
  292. /// <remarks>
  293. /// </remarks>
  294. public override void RemoveAll ()
  295. {
  296. contentView.RemoveAll ();
  297. }
  298. /// <summary>
  299. /// Gets or sets the visibility for the vertical scroll indicator.
  300. /// </summary>
  301. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  302. public bool ShowVerticalScrollIndicator {
  303. get => showVerticalScrollIndicator;
  304. set {
  305. if (value != showVerticalScrollIndicator) {
  306. showVerticalScrollIndicator = value;
  307. SetNeedsLayout ();
  308. if (value) {
  309. vertical.OtherScrollBarView = horizontal;
  310. base.Add (vertical);
  311. vertical.ShowScrollIndicator = value;
  312. vertical.AutoHideScrollBars = autoHideScrollBars;
  313. vertical.OtherScrollBarView.ShowScrollIndicator = value;
  314. vertical.MouseEnter += View_MouseEnter;
  315. vertical.MouseLeave += View_MouseLeave;
  316. } else {
  317. Remove (vertical);
  318. vertical.OtherScrollBarView = null;
  319. vertical.MouseEnter -= View_MouseEnter;
  320. vertical.MouseLeave -= View_MouseLeave;
  321. }
  322. }
  323. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  324. }
  325. }
  326. /// <inheritdoc/>
  327. public override void OnDrawContent (Rect contentArea)
  328. {
  329. SetViewsNeedsDisplay ();
  330. var savedClip = ClipToBounds ();
  331. Driver.SetAttribute (GetNormalColor ());
  332. Clear ();
  333. contentView.Draw ();
  334. if (autoHideScrollBars) {
  335. ShowHideScrollBars ();
  336. } else {
  337. if (ShowVerticalScrollIndicator) {
  338. //vertical.SetRelativeLayout (Bounds);
  339. vertical.Draw ();
  340. }
  341. if (ShowHorizontalScrollIndicator) {
  342. //horizontal.SetRelativeLayout (Bounds);
  343. horizontal.Draw ();
  344. }
  345. }
  346. // Fill in the bottom left corner. Note we don't rely on ScrollBarView.contentBottomRightCorner here
  347. // because that only applies when ScrollBarView is hosted.
  348. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  349. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  350. }
  351. Driver.SetAttribute (GetNormalColor ());
  352. Driver.Clip = savedClip;
  353. }
  354. void ShowHideScrollBars ()
  355. {
  356. bool v = false, h = false; bool p = false;
  357. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  358. if (ShowVerticalScrollIndicator) {
  359. ShowVerticalScrollIndicator = false;
  360. }
  361. v = false;
  362. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  363. p = true;
  364. } else {
  365. if (!ShowVerticalScrollIndicator) {
  366. ShowVerticalScrollIndicator = true;
  367. }
  368. v = true;
  369. }
  370. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  371. if (ShowHorizontalScrollIndicator) {
  372. ShowHorizontalScrollIndicator = false;
  373. }
  374. h = false;
  375. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  376. if (ShowHorizontalScrollIndicator) {
  377. ShowHorizontalScrollIndicator = false;
  378. }
  379. h = false;
  380. if (ShowVerticalScrollIndicator) {
  381. ShowVerticalScrollIndicator = false;
  382. }
  383. v = false;
  384. } else {
  385. if (p) {
  386. if (!ShowVerticalScrollIndicator) {
  387. ShowVerticalScrollIndicator = true;
  388. }
  389. v = true;
  390. }
  391. if (!ShowHorizontalScrollIndicator) {
  392. ShowHorizontalScrollIndicator = true;
  393. }
  394. h = true;
  395. }
  396. var dim = Dim.Fill (h ? 1 : 0);
  397. if (!vertical.Height.Equals (dim)) {
  398. vertical.Height = dim;
  399. }
  400. dim = Dim.Fill (v ? 1 : 0);
  401. if (!horizontal.Width.Equals (dim)) {
  402. horizontal.Width = dim;
  403. }
  404. if (v) {
  405. vertical.SetRelativeLayout (Bounds);
  406. vertical.Draw ();
  407. }
  408. if (h) {
  409. horizontal.SetRelativeLayout (Bounds);
  410. horizontal.Draw ();
  411. }
  412. }
  413. void SetViewsNeedsDisplay ()
  414. {
  415. foreach (View view in contentView.Subviews) {
  416. view.SetNeedsDisplay ();
  417. }
  418. }
  419. ///<inheritdoc/>
  420. public override void PositionCursor ()
  421. {
  422. if (InternalSubviews.Count == 0)
  423. Move (0, 0);
  424. else
  425. base.PositionCursor ();
  426. }
  427. /// <summary>
  428. /// Scrolls the view up.
  429. /// </summary>
  430. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  431. /// <param name="lines">Number of lines to scroll.</param>
  432. public bool ScrollUp (int lines)
  433. {
  434. if (contentOffset.Y < 0) {
  435. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  436. return true;
  437. }
  438. return false;
  439. }
  440. /// <summary>
  441. /// Scrolls the view to the left
  442. /// </summary>
  443. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  444. /// <param name="cols">Number of columns to scroll by.</param>
  445. public bool ScrollLeft (int cols)
  446. {
  447. if (contentOffset.X < 0) {
  448. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  449. return true;
  450. }
  451. return false;
  452. }
  453. /// <summary>
  454. /// Scrolls the view down.
  455. /// </summary>
  456. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  457. /// <param name="lines">Number of lines to scroll.</param>
  458. public bool ScrollDown (int lines)
  459. {
  460. if (vertical.CanScroll (lines, out _, true)) {
  461. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  462. return true;
  463. }
  464. return false;
  465. }
  466. /// <summary>
  467. /// Scrolls the view to the right.
  468. /// </summary>
  469. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  470. /// <param name="cols">Number of columns to scroll by.</param>
  471. public bool ScrollRight (int cols)
  472. {
  473. if (horizontal.CanScroll (cols, out _)) {
  474. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  475. return true;
  476. }
  477. return false;
  478. }
  479. ///<inheritdoc/>
  480. public override bool ProcessKey (KeyEvent kb)
  481. {
  482. if (base.ProcessKey (kb))
  483. return true;
  484. var result = InvokeKeybindings (kb);
  485. if (result != null)
  486. return (bool)result;
  487. return false;
  488. }
  489. ///<inheritdoc/>
  490. public override bool MouseEvent (MouseEvent me)
  491. {
  492. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  493. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  494. // me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  495. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  496. return false;
  497. }
  498. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  499. ScrollDown (1);
  500. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  501. ScrollUp (1);
  502. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  503. ScrollRight (1);
  504. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  505. ScrollLeft (1);
  506. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  507. vertical.MouseEvent (me);
  508. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  509. horizontal.MouseEvent (me);
  510. } else if (IsOverridden (me.View, "MouseEvent")) {
  511. Application.UngrabMouse ();
  512. }
  513. return true;
  514. }
  515. ///<inheritdoc/>
  516. protected override void Dispose (bool disposing)
  517. {
  518. if (!showVerticalScrollIndicator) {
  519. // It was not added to SuperView, so it won't get disposed automatically
  520. vertical?.Dispose ();
  521. }
  522. if (!showHorizontalScrollIndicator) {
  523. // It was not added to SuperView, so it won't get disposed automatically
  524. horizontal?.Dispose ();
  525. }
  526. base.Dispose (disposing);
  527. }
  528. ///<inheritdoc/>
  529. public override bool OnEnter (View view)
  530. {
  531. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  532. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  533. }
  534. return base.OnEnter (view);
  535. }
  536. }
  537. }