ScrollView.cs 17 KB

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