ScrollView.cs 16 KB

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