ScrollView.cs 14 KB

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