2
0

ScrollView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. /// <summary>
  221. /// Removes the view from the scrollview.
  222. /// </summary>
  223. /// <param name="view">The view to remove from the scrollview.</param>
  224. public override void Remove (View view)
  225. {
  226. if (view == null) {
  227. return;
  228. }
  229. SetNeedsDisplay ();
  230. var container = view?.SuperView;
  231. if (container == this) {
  232. base.Remove (view);
  233. } else {
  234. container?.Remove (view);
  235. }
  236. if (contentView.InternalSubviews.Count < 1) {
  237. this.CanFocus = false;
  238. }
  239. }
  240. /// <summary>
  241. /// Removes all widgets from this container.
  242. /// </summary>
  243. /// <remarks>
  244. /// </remarks>
  245. public override void RemoveAll ()
  246. {
  247. contentView.RemoveAll ();
  248. }
  249. void View_MouseLeave (MouseEventArgs e)
  250. {
  251. if (Application.MouseGrabView != null && Application.MouseGrabView != vertical && Application.MouseGrabView != horizontal) {
  252. Application.UngrabMouse ();
  253. }
  254. }
  255. void View_MouseEnter (MouseEventArgs e)
  256. {
  257. Application.GrabMouse (this);
  258. }
  259. /// <summary>
  260. /// Gets or sets the visibility for the horizontal scroll indicator.
  261. /// </summary>
  262. /// <value><c>true</c> if show horizontal scroll indicator; otherwise, <c>false</c>.</value>
  263. public bool ShowHorizontalScrollIndicator {
  264. get => showHorizontalScrollIndicator;
  265. set {
  266. if (value == showHorizontalScrollIndicator) {
  267. return;
  268. }
  269. showHorizontalScrollIndicator = value;
  270. SetNeedsLayout ();
  271. if (value) {
  272. base.Add (horizontal);
  273. horizontal.ShowScrollIndicator = value;
  274. horizontal.AutoHideScrollBars = autoHideScrollBars;
  275. horizontal.OtherScrollBarView = vertical;
  276. horizontal.OtherScrollBarView.ShowScrollIndicator = value;
  277. horizontal.MouseEnter += View_MouseEnter;
  278. horizontal.MouseLeave += View_MouseLeave;
  279. } else {
  280. base.Remove (horizontal);
  281. horizontal.OtherScrollBarView = null;
  282. horizontal.MouseEnter -= View_MouseEnter;
  283. horizontal.MouseLeave -= View_MouseLeave;
  284. }
  285. vertical.Height = Dim.Fill (showHorizontalScrollIndicator ? 1 : 0);
  286. }
  287. }
  288. /// <summary>
  289. /// Gets or sets the visibility for the vertical scroll indicator.
  290. /// </summary>
  291. /// <value><c>true</c> if show vertical scroll indicator; otherwise, <c>false</c>.</value>
  292. public bool ShowVerticalScrollIndicator {
  293. get => showVerticalScrollIndicator;
  294. set {
  295. if (value == showVerticalScrollIndicator) {
  296. return;
  297. }
  298. showVerticalScrollIndicator = value;
  299. SetNeedsLayout ();
  300. if (value) {
  301. base.Add (vertical);
  302. vertical.ShowScrollIndicator = value;
  303. vertical.AutoHideScrollBars = autoHideScrollBars;
  304. vertical.OtherScrollBarView = horizontal;
  305. vertical.OtherScrollBarView.ShowScrollIndicator = value;
  306. vertical.MouseEnter += View_MouseEnter;
  307. vertical.MouseLeave += View_MouseLeave;
  308. } else {
  309. Remove (vertical);
  310. vertical.OtherScrollBarView = null;
  311. vertical.MouseEnter -= View_MouseEnter;
  312. vertical.MouseLeave -= View_MouseLeave;
  313. }
  314. horizontal.Width = Dim.Fill (showVerticalScrollIndicator ? 1 : 0);
  315. }
  316. }
  317. /// <inheritdoc/>
  318. public override void Redraw (Rect region)
  319. {
  320. Driver.SetAttribute (GetNormalColor ());
  321. SetViewsNeedsDisplay ();
  322. Clear ();
  323. var savedClip = ClipToBounds ();
  324. OnDrawContent (new Rect (ContentOffset,
  325. new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
  326. Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0))));
  327. contentView.Redraw (contentView.Frame);
  328. Driver.Clip = savedClip;
  329. if (autoHideScrollBars) {
  330. ShowHideScrollBars ();
  331. } else {
  332. if (ShowVerticalScrollIndicator) {
  333. vertical.SetRelativeLayout (Bounds);
  334. vertical.Redraw (vertical.Bounds);
  335. }
  336. if (ShowHorizontalScrollIndicator) {
  337. horizontal.SetRelativeLayout (Bounds);
  338. horizontal.Redraw (horizontal.Bounds);
  339. }
  340. }
  341. // Fill in the bottom right corner
  342. if (ShowVerticalScrollIndicator && ShowHorizontalScrollIndicator) {
  343. AddRune (Bounds.Width - 1, Bounds.Height - 1, ' ');
  344. }
  345. Driver.SetAttribute (GetNormalColor ());
  346. }
  347. void ShowHideScrollBars ()
  348. {
  349. bool v = false, h = false; bool p = false;
  350. if (Bounds.Height == 0 || Bounds.Height > contentSize.Height) {
  351. if (ShowVerticalScrollIndicator) {
  352. ShowVerticalScrollIndicator = false;
  353. }
  354. v = false;
  355. } else if (Bounds.Height > 0 && Bounds.Height == contentSize.Height) {
  356. p = true;
  357. } else {
  358. if (!ShowVerticalScrollIndicator) {
  359. ShowVerticalScrollIndicator = true;
  360. }
  361. v = true;
  362. }
  363. if (Bounds.Width == 0 || Bounds.Width > contentSize.Width) {
  364. if (ShowHorizontalScrollIndicator) {
  365. ShowHorizontalScrollIndicator = false;
  366. }
  367. h = false;
  368. } else if (Bounds.Width > 0 && Bounds.Width == contentSize.Width && p) {
  369. if (ShowHorizontalScrollIndicator) {
  370. ShowHorizontalScrollIndicator = false;
  371. }
  372. h = false;
  373. if (ShowVerticalScrollIndicator) {
  374. ShowVerticalScrollIndicator = false;
  375. }
  376. v = false;
  377. } else {
  378. if (p) {
  379. if (!ShowVerticalScrollIndicator) {
  380. ShowVerticalScrollIndicator = true;
  381. }
  382. v = true;
  383. }
  384. if (!ShowHorizontalScrollIndicator) {
  385. ShowHorizontalScrollIndicator = true;
  386. }
  387. h = true;
  388. }
  389. var dim = Dim.Fill (h ? 1 : 0);
  390. if (!vertical.Height.Equals (dim)) {
  391. vertical.Height = dim;
  392. }
  393. dim = Dim.Fill (v ? 1 : 0);
  394. if (!horizontal.Width.Equals (dim)) {
  395. horizontal.Width = dim;
  396. }
  397. if (v) {
  398. vertical.SetRelativeLayout (Bounds);
  399. vertical.Redraw (vertical.Bounds);
  400. }
  401. if (h) {
  402. horizontal.SetRelativeLayout (Bounds);
  403. horizontal.Redraw (horizontal.Bounds);
  404. }
  405. }
  406. void SetViewsNeedsDisplay ()
  407. {
  408. foreach (View view in contentView.Subviews) {
  409. view.SetNeedsDisplay ();
  410. }
  411. }
  412. ///<inheritdoc/>
  413. public override void PositionCursor ()
  414. {
  415. if (InternalSubviews.Count == 0)
  416. Move (0, 0);
  417. else
  418. base.PositionCursor ();
  419. }
  420. /// <summary>
  421. /// Scrolls the view up.
  422. /// </summary>
  423. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  424. /// <param name="lines">Number of lines to scroll.</param>
  425. public bool ScrollUp (int lines)
  426. {
  427. if (contentOffset.Y < 0) {
  428. ContentOffset = new Point (contentOffset.X, Math.Min (contentOffset.Y + lines, 0));
  429. return true;
  430. }
  431. return false;
  432. }
  433. /// <summary>
  434. /// Scrolls the view to the left
  435. /// </summary>
  436. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  437. /// <param name="cols">Number of columns to scroll by.</param>
  438. public bool ScrollLeft (int cols)
  439. {
  440. if (contentOffset.X < 0) {
  441. ContentOffset = new Point (Math.Min (contentOffset.X + cols, 0), contentOffset.Y);
  442. return true;
  443. }
  444. return false;
  445. }
  446. /// <summary>
  447. /// Scrolls the view down.
  448. /// </summary>
  449. /// <returns><c>true</c>, if left was scrolled, <c>false</c> otherwise.</returns>
  450. /// <param name="lines">Number of lines to scroll.</param>
  451. public bool ScrollDown (int lines)
  452. {
  453. if (vertical.CanScroll (lines, out _, true)) {
  454. ContentOffset = new Point (contentOffset.X, contentOffset.Y - lines);
  455. return true;
  456. }
  457. return false;
  458. }
  459. /// <summary>
  460. /// Scrolls the view to the right.
  461. /// </summary>
  462. /// <returns><c>true</c>, if right was scrolled, <c>false</c> otherwise.</returns>
  463. /// <param name="cols">Number of columns to scroll by.</param>
  464. public bool ScrollRight (int cols)
  465. {
  466. if (horizontal.CanScroll (cols, out _)) {
  467. ContentOffset = new Point (contentOffset.X - cols, contentOffset.Y);
  468. return true;
  469. }
  470. return false;
  471. }
  472. ///<inheritdoc/>
  473. public override bool ProcessKey (KeyEvent kb)
  474. {
  475. if (base.ProcessKey (kb))
  476. return true;
  477. var result = InvokeKeybindings (kb);
  478. if (result != null)
  479. return (bool)result;
  480. return false;
  481. }
  482. ///<inheritdoc/>
  483. public override bool MouseEvent (MouseEvent me)
  484. {
  485. if (me.Flags != MouseFlags.WheeledDown && me.Flags != MouseFlags.WheeledUp &&
  486. me.Flags != MouseFlags.WheeledRight && me.Flags != MouseFlags.WheeledLeft &&
  487. // me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  488. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  489. return false;
  490. }
  491. if (me.Flags == MouseFlags.WheeledDown && ShowVerticalScrollIndicator) {
  492. ScrollDown (1);
  493. } else if (me.Flags == MouseFlags.WheeledUp && ShowVerticalScrollIndicator) {
  494. ScrollUp (1);
  495. } else if (me.Flags == MouseFlags.WheeledRight && showHorizontalScrollIndicator) {
  496. ScrollRight (1);
  497. } else if (me.Flags == MouseFlags.WheeledLeft && ShowVerticalScrollIndicator) {
  498. ScrollLeft (1);
  499. } else if (me.X == vertical.Frame.X && ShowVerticalScrollIndicator) {
  500. vertical.MouseEvent (me);
  501. } else if (me.Y == horizontal.Frame.Y && ShowHorizontalScrollIndicator) {
  502. horizontal.MouseEvent (me);
  503. } else if (IsOverridden (me.View, "MouseEvent")) {
  504. Application.UngrabMouse ();
  505. }
  506. return true;
  507. }
  508. ///<inheritdoc/>
  509. protected override void Dispose (bool disposing)
  510. {
  511. if (!showVerticalScrollIndicator) {
  512. // It was not added to SuperView, so it won't get disposed automatically
  513. vertical?.Dispose ();
  514. }
  515. if (!showHorizontalScrollIndicator) {
  516. // It was not added to SuperView, so it won't get disposed automatically
  517. horizontal?.Dispose ();
  518. }
  519. base.Dispose (disposing);
  520. }
  521. ///<inheritdoc/>
  522. public override bool OnEnter (View view)
  523. {
  524. if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
  525. Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  526. }
  527. return base.OnEnter (view);
  528. }
  529. }
  530. }