ScrollBarView.cs 17 KB

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