ScrollView.cs 16 KB

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