2
0

ScrollView.cs 16 KB

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