ScrollView.cs 15 KB

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