ScrollView.cs 16 KB

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