ScrollBarView.cs 17 KB

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