ScrollBarView.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // ScrollBarView.cs: ScrollBarView view.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// ScrollBarViews are views that display a 1-character scrollbar, either horizontal or vertical
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// The scrollbar is drawn to be a representation of the Size, assuming that the
  15. /// scroll position is set at Position.
  16. /// </para>
  17. /// <para>
  18. /// If the region to display the scrollbar is larger than three characters,
  19. /// arrow indicators are drawn.
  20. /// </para>
  21. /// </remarks>
  22. public class ScrollBarView : View {
  23. bool vertical;
  24. int size, position, contentOffset;
  25. bool showScrollIndicator;
  26. bool keepContentAlwaysInViewport = true;
  27. bool autoHideScrollBars = true;
  28. Dim originalHostWidth, originalHostHeight;
  29. bool hosted;
  30. bool showBothScrollIndicator => OtherScrollBarView != null && OtherScrollBarView.showScrollIndicator && showScrollIndicator;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  33. /// </summary>
  34. /// <param name="rect">Frame for the scrollbar.</param>
  35. public ScrollBarView (Rect rect) : this (rect, 0, 0, false) { }
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
  38. /// </summary>
  39. /// <param name="rect">Frame for the scrollbar.</param>
  40. /// <param name="size">The size that this scrollbar represents. Sets the <see cref="Size"/> property.</param>
  41. /// <param name="position">The position within this scrollbar. Sets the <see cref="Position"/> property.</param>
  42. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal. Sets the <see cref="IsVertical"/> property.</param>
  43. public ScrollBarView (Rect rect, int size, int position, bool isVertical) : base (rect)
  44. {
  45. Init (size, position, isVertical);
  46. }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  49. /// </summary>
  50. public ScrollBarView () : this (0, 0, false) { }
  51. /// <summary>
  52. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  53. /// </summary>
  54. /// <param name="size">The size that this scrollbar represents.</param>
  55. /// <param name="position">The position within this scrollbar.</param>
  56. /// <param name="isVertical">If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.</param>
  57. public ScrollBarView (int size, int position, bool isVertical) : base ()
  58. {
  59. Init (size, position, isVertical);
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="Gui.ScrollBarView"/> class using <see cref="LayoutStyle.Computed"/> layout.
  63. /// </summary>
  64. public ScrollBarView (View host, bool isVertical) : this (0, 0, isVertical)
  65. {
  66. hosted = true;
  67. originalHostWidth = host.Width;
  68. originalHostHeight = host.Height;
  69. X = isVertical ? Pos.Right(host) : Pos.Left (host);
  70. Y = isVertical ? Pos.Top (host) : Pos.Bottom (host);
  71. Host = host;
  72. Host.SuperView.Add (this);
  73. ShowScrollIndicator = true;
  74. AutoHideScrollBars = true;
  75. }
  76. void Init (int size, int position, bool isVertical)
  77. {
  78. vertical = isVertical;
  79. this.position = position;
  80. this.size = size;
  81. WantContinuousButtonPressed = true;
  82. }
  83. /// <summary>
  84. /// If set to <c>true</c> this is a vertical scrollbar, otherwise, the scrollbar is horizontal.
  85. /// </summary>
  86. public bool IsVertical {
  87. get => vertical;
  88. set {
  89. vertical = value;
  90. SetNeedsDisplay ();
  91. }
  92. }
  93. /// <summary>
  94. /// The size of content the scrollbar represents.
  95. /// </summary>
  96. /// <value>The size.</value>
  97. /// <remarks>The <see cref="Size"/> is typically the size of the virtual content. E.g. when a Scrollbar is
  98. /// part of a <see cref="View"/> the Size is set to the appropriate dimension of <see cref="Host"/>.</remarks>
  99. public int Size {
  100. get => size;
  101. set {
  102. size = value;
  103. ShowHideScrollBars ();
  104. SetNeedsDisplay ();
  105. }
  106. }
  107. /// <summary>
  108. /// Represents the top left/top corner coordinate that is displayed by the scrollbar.
  109. /// </summary>
  110. /// <value>The content offset.</value>
  111. public int ContentOffset {
  112. get {
  113. return contentOffset;
  114. }
  115. set {
  116. var co = -Math.Abs (value);
  117. if (contentOffset != co) {
  118. if (CanScroll (value + contentOffset, out int max, vertical) || max > 0) {
  119. if (max == contentOffset - co) {
  120. contentOffset = co;
  121. } else {
  122. contentOffset = -max;
  123. }
  124. }
  125. Position = Math.Max (0, -contentOffset);
  126. OnChangedPosition ();
  127. SetNeedsDisplay ();
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// This event is raised when the position on the scrollbar has changed.
  133. /// </summary>
  134. public event Action ChangedPosition;
  135. /// <summary>
  136. /// The position, relative to <see cref="Size"/>, to set the scrollbar at.
  137. /// </summary>
  138. /// <value>The position.</value>
  139. public int Position {
  140. get => position;
  141. set {
  142. position = value;
  143. SetNeedsDisplay ();
  144. }
  145. }
  146. /// <summary>
  147. /// Get or sets the view that host this <see cref="View"/>
  148. /// </summary>
  149. public View Host { get; internal set; }
  150. /// <summary>
  151. /// Represent a vertical or horizontal ScrollBarView other than this.
  152. /// </summary>
  153. public ScrollBarView OtherScrollBarView { get; set; }
  154. /// <summary>
  155. /// Gets or sets the visibility for the vertical or horizontal scroll indicator.
  156. /// </summary>
  157. /// <value><c>true</c> if show vertical or horizontal scroll indicator; otherwise, <c>false</c>.</value>
  158. public bool ShowScrollIndicator {
  159. get => showScrollIndicator;
  160. set {
  161. if (value == showScrollIndicator) {
  162. return;
  163. }
  164. showScrollIndicator = value;
  165. SetNeedsLayout ();
  166. if (value) {
  167. Visible = true;
  168. } else {
  169. Visible = false;
  170. }
  171. Width = vertical ? 1 : Dim.Width (Host);
  172. Height = vertical ? Dim.Height (Host) : 1;
  173. if (vertical) {
  174. Host.Width = showScrollIndicator ? originalHostWidth - 1 : originalHostWidth;
  175. } else {
  176. Host.Height = showScrollIndicator ? originalHostHeight - 1 : originalHostHeight;
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Get or sets if the view-port is kept always visible in the area of this <see cref="ScrollBarView"/>
  182. /// </summary>
  183. public bool KeepContentAlwaysInViewport {
  184. get { return keepContentAlwaysInViewport; }
  185. set {
  186. if (keepContentAlwaysInViewport != value) {
  187. keepContentAlwaysInViewport = value;
  188. int co = 0;
  189. if (value && !vertical && -contentOffset + Host.Bounds.Width > size) {
  190. co = size - Host.Bounds.Width + (showBothScrollIndicator ? 1 : 0);
  191. }
  192. if (value && vertical && -contentOffset + Host.Bounds.Height > size) {
  193. co = size - Host.Bounds.Height + (showBothScrollIndicator ? 1 : 0);
  194. }
  195. if (co != 0) {
  196. ContentOffset = co;
  197. }
  198. if (OtherScrollBarView != null && OtherScrollBarView.keepContentAlwaysInViewport != value) {
  199. OtherScrollBarView.KeepContentAlwaysInViewport = value;
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// If true the vertical/horizontal scroll bars won't be showed if it's not needed.
  206. /// </summary>
  207. public bool AutoHideScrollBars {
  208. get => autoHideScrollBars;
  209. set {
  210. if (autoHideScrollBars != value) {
  211. autoHideScrollBars = value;
  212. SetNeedsDisplay ();
  213. }
  214. }
  215. }
  216. void SetPosition (int newPos)
  217. {
  218. Position = newPos;
  219. ContentOffset = Position;
  220. OnChangedPosition ();
  221. }
  222. /// <summary>
  223. /// Virtual method to invoke the <see cref="ChangedPosition"/> action event.
  224. /// </summary>
  225. public virtual void OnChangedPosition ()
  226. {
  227. ChangedPosition?.Invoke ();
  228. }
  229. internal bool pending;
  230. void ShowHideScrollBars ()
  231. {
  232. if (!hosted || !autoHideScrollBars) {
  233. return;
  234. }
  235. int barsize = vertical ? Bounds.Height : Bounds.Width;
  236. if (barsize == 0 || barsize > size) {
  237. if (showScrollIndicator) {
  238. ShowScrollIndicator = false;
  239. }
  240. } else if (barsize > 0 && barsize == size && OtherScrollBarView != null && OtherScrollBarView.pending) {
  241. if (showScrollIndicator) {
  242. ShowScrollIndicator = false;
  243. }
  244. if (OtherScrollBarView != null && showBothScrollIndicator) {
  245. OtherScrollBarView.ShowScrollIndicator = false;
  246. }
  247. } else if (barsize > 0 && barsize == size && OtherScrollBarView != null && !OtherScrollBarView.pending) {
  248. pending = true;
  249. OtherScrollBarView.Redraw (OtherScrollBarView.Bounds);
  250. } else {
  251. if (OtherScrollBarView != null && OtherScrollBarView.pending) {
  252. if (!showBothScrollIndicator) {
  253. OtherScrollBarView.ShowScrollIndicator = true;
  254. OtherScrollBarView.Redraw (OtherScrollBarView.Bounds);
  255. }
  256. }
  257. if (!showScrollIndicator) {
  258. ShowScrollIndicator = true;
  259. }
  260. }
  261. if (OtherScrollBarView != null) {
  262. OtherScrollBarView.pending = false;
  263. }
  264. }
  265. int posTopTee;
  266. int posLeftTee;
  267. int posBottomTee;
  268. int posRightTee;
  269. ///<inheritdoc/>
  270. public override void Redraw (Rect region)
  271. {
  272. if (ColorScheme == null || Size == 0) {
  273. return;
  274. }
  275. Driver.SetAttribute (ColorScheme.Normal);
  276. if ((vertical && Bounds.Height == 0) || (!vertical && Bounds.Width == 0)) {
  277. return;
  278. }
  279. if (vertical) {
  280. if (region.Right < Bounds.Width - 1) {
  281. return;
  282. }
  283. var col = Bounds.Width - 1;
  284. var bh = Bounds.Height;
  285. Rune special;
  286. if (bh < 4) {
  287. var by1 = position * bh / Size;
  288. var by2 = (position + bh) * bh / Size;
  289. Move (col, 0);
  290. if (Bounds.Height == 1) {
  291. Driver.AddRune (Driver.Diamond);
  292. } else {
  293. Driver.AddRune (Driver.UpArrow);
  294. }
  295. if (Bounds.Height == 3) {
  296. Move (col, 1);
  297. Driver.AddRune (Driver.Diamond);
  298. }
  299. if (Bounds.Height > 1) {
  300. Move (col, Bounds.Height - 1);
  301. Driver.AddRune (Driver.DownArrow);
  302. }
  303. } else {
  304. bh -= 2;
  305. var by1 = position * bh / Size;
  306. var by2 = KeepContentAlwaysInViewport ? Math.Min (((position + bh) * bh / Size) + 1, bh - 1) : (position + bh) * bh / Size;
  307. if (KeepContentAlwaysInViewport && by1 == by2) {
  308. by1 = Math.Max (by1 - 1, 0);
  309. }
  310. Move (col, 0);
  311. Driver.AddRune (Driver.UpArrow);
  312. Move (col, Bounds.Height - 1);
  313. Driver.AddRune (Driver.DownArrow);
  314. bool hasTopTee = false;
  315. bool hasDiamond = false;
  316. bool hasBottomTee = false;
  317. for (int y = 0; y < bh; y++) {
  318. Move (col, y + 1);
  319. if ((y < by1 || y > by2) && ((position > 0 && !hasTopTee) || (hasTopTee && hasBottomTee))) {
  320. special = Driver.Stipple;
  321. } else {
  322. if (y != by2 && y > 1 && by2 - by1 == 0 && by1 < bh - 1 && hasTopTee && !hasDiamond) {
  323. hasDiamond = true;
  324. special = Driver.Diamond;
  325. } else {
  326. if (y == by1 && !hasTopTee) {
  327. hasTopTee = true;
  328. posTopTee = y;
  329. special = Driver.TopTee;
  330. } else if ((position == 0 && y == bh - 1 || y >= by2 || by2 == 0) && !hasBottomTee) {
  331. hasBottomTee = true;
  332. posBottomTee = y;
  333. special = Driver.BottomTee;
  334. } else {
  335. special = Driver.VLine;
  336. }
  337. }
  338. }
  339. Driver.AddRune (special);
  340. }
  341. if (!hasTopTee) {
  342. Move (col, Bounds.Height - 2);
  343. Driver.AddRune (Driver.TopTee);
  344. }
  345. }
  346. } else {
  347. if (region.Bottom < Bounds.Height - 1) {
  348. return;
  349. }
  350. var row = Bounds.Height - 1;
  351. var bw = Bounds.Width;
  352. Rune special;
  353. if (bw < 4) {
  354. var bx1 = position * bw / Size;
  355. var bx2 = (position + bw) * bw / Size;
  356. Move (0, row);
  357. Driver.AddRune (Driver.LeftArrow);
  358. Driver.AddRune (Driver.RightArrow);
  359. } else {
  360. bw -= 2;
  361. var bx1 = position * bw / Size;
  362. var bx2 = KeepContentAlwaysInViewport ? Math.Min (((position + bw) * bw / Size) + 1, bw - 1) : (position + bw) * bw / Size;
  363. if (KeepContentAlwaysInViewport && bx1 == bx2) {
  364. bx1 = Math.Max (bx1 - 1, 0);
  365. }
  366. Move (0, row);
  367. Driver.AddRune (Driver.LeftArrow);
  368. bool hasLeftTee = false;
  369. bool hasDiamond = false;
  370. bool hasRightTee = false;
  371. for (int x = 0; x < bw; x++) {
  372. if ((x < bx1 || x >= bx2 + 1) && ((position > 0 && !hasLeftTee) || (hasLeftTee && hasRightTee))) {
  373. special = Driver.Stipple;
  374. } else {
  375. if (x != bx2 && x > 1 && bx2 - bx1 == 0 && bx1 < bw - 1 && hasLeftTee && !hasDiamond) {
  376. hasDiamond = true;
  377. special = Driver.Diamond;
  378. } else {
  379. if (x == bx1 && !hasLeftTee) {
  380. hasLeftTee = true;
  381. posLeftTee = x;
  382. special = Driver.LeftTee;
  383. } else if ((position == 0 && x == bw - 1 || x >= bx2 || bx2 == 0) && !hasRightTee) {
  384. hasRightTee = true;
  385. posRightTee = x;
  386. special = Driver.RightTee;
  387. } else {
  388. special = Driver.HLine;
  389. }
  390. }
  391. }
  392. Driver.AddRune (special);
  393. }
  394. if (!hasLeftTee) {
  395. Move (Bounds.Width -2, row);
  396. Driver.AddRune (Driver.LeftTee);
  397. }
  398. Driver.AddRune (Driver.RightArrow);
  399. }
  400. }
  401. }
  402. int lastLocation = -1;
  403. ///<inheritdoc/>
  404. public override bool MouseEvent (MouseEvent me)
  405. {
  406. if (me.Flags != MouseFlags.Button1Pressed && me.Flags != MouseFlags.Button1Clicked &&
  407. !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  408. return false;
  409. }
  410. if (!me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  411. lastLocation = -1;
  412. }
  413. int location = vertical ? me.Y : me.X;
  414. int barsize = vertical ? Bounds.Height : Bounds.Width;
  415. int posTopLeftTee = vertical ? posTopTee : posLeftTee;
  416. int posBottomRightTee = vertical ? posBottomTee : posRightTee;
  417. barsize -= 2;
  418. var pos = Position;
  419. if (location == 0) {
  420. if (pos > 0) {
  421. SetPosition (pos - 1);
  422. }
  423. } else if (location == barsize + 1) {
  424. if (CanScroll (1, out _, vertical)) {
  425. SetPosition (pos + 1);
  426. }
  427. } else if (location > 0 && location < barsize + 1) {
  428. var b1 = pos * barsize / Size;
  429. var b2 = KeepContentAlwaysInViewport ? Math.Min (((pos + barsize) * barsize / Size) + 1, barsize - 1) : (pos + barsize) * barsize / Size;
  430. if (KeepContentAlwaysInViewport && b1 == b2) {
  431. b1 = Math.Max (b1 - 1, 0);
  432. }
  433. if (location > b1 && location <= b2 + 1) {
  434. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1Clicked) {
  435. if (location == 1) {
  436. SetPosition (0);
  437. } else if (location == barsize) {
  438. CanScroll (Size - pos, out int nv, vertical);
  439. if (nv > 0) {
  440. SetPosition (Math.Min (pos + nv, Size));
  441. }
  442. }
  443. } else if (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  444. var mb = (b2 - b1) / 2;
  445. var ml = mb + b1 + (mb == 0 ? 1 : 0);
  446. if ((location >= b1 && location <= ml) || (location < lastLocation && lastLocation > -1)) {
  447. lastLocation = location;
  448. var np = b1 * Size / barsize;
  449. SetPosition (np);
  450. } else if (location > lastLocation) {
  451. var np = location * Size / barsize;
  452. CanScroll (np - pos, out int nv, vertical);
  453. if (nv > 0) {
  454. SetPosition (pos + nv);
  455. }
  456. }
  457. }
  458. } else {
  459. if (location >= b2 + 1 && location > posTopLeftTee && location > b1 && location > posBottomRightTee && posBottomRightTee > 0) {
  460. CanScroll (location, out int nv, vertical);
  461. if (nv > 0) {
  462. SetPosition (Math.Min (pos + nv, Size));
  463. }
  464. } else if (location <= b1) {
  465. SetPosition (Math.Max (pos - barsize - location, 0));
  466. }
  467. }
  468. }
  469. return true;
  470. }
  471. internal bool CanScroll (int n, out int max, bool isVertical = false)
  472. {
  473. var size = isVertical ?
  474. (KeepContentAlwaysInViewport ? Host.Bounds.Height + (showBothScrollIndicator ? -2 : -1) : 0) :
  475. (KeepContentAlwaysInViewport ? Host.Bounds.Width + (showBothScrollIndicator ? -2 : -1) : 0);
  476. var cSize = -Size;
  477. var cOffSet = contentOffset;
  478. var newSize = Math.Max (cSize, cOffSet - n);
  479. max = cSize < newSize - size ? n : -cSize + (cOffSet - size) - 1;
  480. if (cSize < newSize - size) {
  481. return true;
  482. }
  483. return false;
  484. }
  485. }
  486. }