ScrollBarView.cs 16 KB

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