ScrollView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 (!IsOverridden (view, "MouseEvent")) {
  241. view.MouseEnter += View_MouseEnter;
  242. view.MouseLeave += View_MouseLeave;
  243. }
  244. contentView.Add (view);
  245. SetNeedsLayout ();
  246. }
  247. void View_MouseLeave (object sender, MouseEventEventArgs e)
  248. {
  249. if (Application.MouseGrabView != null && Application.MouseGrabView != vertical && Application.MouseGrabView != horizontal) {
  250. Application.UngrabMouse ();
  251. }
  252. }
  253. void View_MouseEnter (object sender, MouseEventEventArgs e)
  254. {
  255. Application.GrabMouse (this);
  256. }
  257. /// <summary>
  258. /// Gets or sets the visibility for the horizontal scroll indicator.
  259. /// </summary>
  260. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  261. public bool ShowHorizontalScrollIndicator {
  262. get => showHorizontalScrollIndicator;
  263. set {
  264. if (value == showHorizontalScrollIndicator) {
  265. return;
  266. }
  267. showHorizontalScrollIndicator = value;
  268. SetNeedsLayout ();
  269. if (value) {
  270. base.Add (horizontal);
  271. horizontal.ShowScrollIndicator = value;
  272. horizontal.AutoHideScrollBars = autoHideScrollBars;
  273. horizontal.OtherScrollBarView = vertical;
  274. horizontal.OtherScrollBarView.ShowScrollIndicator = value;
  275. horizontal.MouseEnter += View_MouseEnter;
  276. horizontal.MouseLeave += View_MouseLeave;
  277. } else {
  278. base.Remove (horizontal);
  279. horizontal.OtherScrollBarView = null;
  280. horizontal.MouseEnter -= View_MouseEnter;
  281. horizontal.MouseLeave -= View_MouseLeave;
  282. }
  283. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  284. }
  285. }
  286. /// <summary>
  287. /// Removes all widgets from this container.
  288. /// </summary>
  289. /// <remarks>
  290. /// </remarks>
  291. public override void RemoveAll ()
  292. {
  293. contentView.RemoveAll ();
  294. }
  295. /// <summary>
  296. /// Gets or sets the visibility for the vertical scroll indicator.
  297. /// </summary>
  298. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  299. public bool ShowVerticalScrollIndicator {
  300. get => showVerticalScrollIndicator;
  301. set {
  302. if (value == showVerticalScrollIndicator) {
  303. return;
  304. }
  305. showVerticalScrollIndicator = value;
  306. SetNeedsLayout ();
  307. if (value) {
  308. base.Add (vertical);
  309. vertical.ShowScrollIndicator = value;
  310. vertical.AutoHideScrollBars = autoHideScrollBars;
  311. vertical.OtherScrollBarView = horizontal;
  312. vertical.OtherScrollBarView.ShowScrollIndicator = value;
  313. vertical.MouseEnter += View_MouseEnter;
  314. vertical.MouseLeave += View_MouseLeave;
  315. } else {
  316. Remove (vertical);
  317. vertical.OtherScrollBarView = null;
  318. vertical.MouseEnter -= View_MouseEnter;
  319. vertical.MouseLeave -= View_MouseLeave;
  320. }
  321. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  322. }
  323. }
  324. /// <inheritdoc/>
  325. public override void Redraw (Rect bounds)
  326. {
  327. SetViewsNeedsDisplay ();
  328. var savedClip = ClipToBounds ();
  329. Driver.SetAttribute (GetNormalColor ());
  330. Clear ();
  331. contentView.Redraw (contentView.Bounds);
  332. OnDrawContent (new Rect (ContentOffset,
  333. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  334. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  335. if (autoHideScrollBars) {
  336. ShowHideScrollBars ();
  337. } else {
  338. if (ShowVerticalScrollIndicator) {
  339. //vertical.SetRelativeLayout (Bounds);
  340. vertical.Redraw (vertical.Bounds);
  341. }
  342. if (ShowHorizontalScrollIndicator) {
  343. //horizontal.SetRelativeLayout (Bounds);
  344. horizontal.Redraw (horizontal.Bounds);
  345. }
  346. }
  347. // Fill in the bottom left corner. Note we don't rely on ScrollBarView.contentBottomRightCorner here
  348. // because that only applies when ScrollBarView is hosted.
  349. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  350. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  351. }
  352. Driver.SetAttribute (GetNormalColor ());
  353. Driver.Clip = savedClip;
  354. }
  355. void ShowHideScrollBars ()
  356. {
  357. bool v = false, h = false; bool p = false;
  358. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  359. if (ShowVerticalScrollIndicator) {
  360. ShowVerticalScrollIndicator = false;
  361. }
  362. v = false;
  363. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  364. p = true;
  365. } else {
  366. if (!ShowVerticalScrollIndicator) {
  367. ShowVerticalScrollIndicator = true;
  368. }
  369. v = true;
  370. }
  371. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  372. if (ShowHorizontalScrollIndicator) {
  373. ShowHorizontalScrollIndicator = false;
  374. }
  375. h = false;
  376. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  377. if (ShowHorizontalScrollIndicator) {
  378. ShowHorizontalScrollIndicator = false;
  379. }
  380. h = false;
  381. if (ShowVerticalScrollIndicator) {
  382. ShowVerticalScrollIndicator = false;
  383. }
  384. v = false;
  385. } else {
  386. if (p) {
  387. if (!ShowVerticalScrollIndicator) {
  388. ShowVerticalScrollIndicator = true;
  389. }
  390. v = true;
  391. }
  392. if (!ShowHorizontalScrollIndicator) {
  393. ShowHorizontalScrollIndicator = true;
  394. }
  395. h = true;
  396. }
  397. var dim = Dim.Fill (h ? 1 : 0);
  398. if (!vertical.Height.Equals (dim)) {
  399. vertical.Height = dim;
  400. }
  401. dim = Dim.Fill (v ? 1 : 0);
  402. if (!horizontal.Width.Equals (dim)) {
  403. horizontal.Width = dim;
  404. }
  405. if (v) {
  406. vertical.SetRelativeLayout (Bounds);
  407. vertical.Redraw (vertical.Bounds);
  408. }
  409. if (h) {
  410. horizontal.SetRelativeLayout (Bounds);
  411. horizontal.Redraw (horizontal.Bounds);
  412. }
  413. }
  414. void SetViewsNeedsDisplay ()
  415. {
  416. foreach (View view in contentView.Subviews) {
  417. view.SetNeedsDisplay ();
  418. }
  419. }
  420. ///<inheritdoc/>
  421. public override void PositionCursor ()
  422. {
  423. if (InternalSubviews.Count == 0)
  424. Move (0, 0);
  425. else
  426. base.PositionCursor ();
  427. }
  428. /// <summary>
  429. /// Scrolls the view up.
  430. /// </summary>
  431. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  432. /// <param name="lines">Number of lines to scroll.</param>
  433. public bool ScrollUp (int lines)
  434. {
  435. if (contentOffset.Y < 0) {
  436. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  437. return true;
  438. }
  439. return false;
  440. }
  441. /// <summary>
  442. /// Scrolls the view to the left
  443. /// </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. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  450. return true;
  451. }
  452. return false;
  453. }
  454. /// <summary>
  455. /// Scrolls the view down.
  456. /// </summary>
  457. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  458. /// <param name="lines">Number of lines to scroll.</param>
  459. public bool ScrollDown (int lines)
  460. {
  461. if (vertical.CanScroll (lines, out _, true)) {
  462. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  463. return true;
  464. }
  465. return false;
  466. }
  467. /// <summary>
  468. /// Scrolls the view to the right.
  469. /// </summary>
  470. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  471. /// <param name="cols">Number of columns to scroll by.</param>
  472. public bool ScrollRight (int cols)
  473. {
  474. if (horizontal.CanScroll (cols, out _)) {
  475. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  476. return true;
  477. }
  478. return false;
  479. }
  480. ///<inheritdoc/>
  481. public override bool ProcessKey (KeyEvent kb)
  482. {
  483. if (base.ProcessKey (kb))
  484. return true;
  485. var result = InvokeKeybindings (kb);
  486. if (result != null)
  487. return (bool)result;
  488. return false;
  489. }
  490. ///<inheritdoc/>
  491. public override bool MouseEvent (MouseEvent me)
  492. {
  493. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  494. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  495. // me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  496. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  497. return false;
  498. }
  499. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  500. ScrollDown (1);
  501. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  502. ScrollUp (1);
  503. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  504. ScrollRight (1);
  505. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  506. ScrollLeft (1);
  507. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  508. vertical.MouseEvent (me);
  509. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  510. horizontal.MouseEvent (me);
  511. } else if (IsOverridden (me.View, "MouseEvent")) {
  512. Application.UngrabMouse ();
  513. }
  514. return true;
  515. }
  516. ///<inheritdoc/>
  517. protected override void Dispose (bool disposing)
  518. {
  519. if (!showVerticalScrollIndicator) {
  520. // It was not added to SuperView, so it won't get disposed automatically
  521. vertical?.Dispose ();
  522. }
  523. if (!showHorizontalScrollIndicator) {
  524. // It was not added to SuperView, so it won't get disposed automatically
  525. horizontal?.Dispose ();
  526. }
  527. base.Dispose (disposing);
  528. }
  529. ///<inheritdoc/>
  530. public override bool OnEnter (View view)
  531. {
  532. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  533. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  534. }
  535. return base.OnEnter (view);
  536. }
  537. }
  538. }