ScrollView.cs 14 KB

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