2
0

ScrollView.cs 15 KB

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