ScrollView.cs 16 KB

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